Date post: | 12-Jul-2015 |
Category: |
Internet |
Upload: | frank-holldorff |
View: | 270 times |
Download: | 0 times |
# Views Plugins in Drupal 7 and Drupal 8
● Michael Lenahan, Developer at erdfisch
● http://drupalcamp.berlin/program/sessions/viewspluginsd7andd8
# Key Points
● In D7, views is extensible through plugins
● In D8, all of Drupal is extensible through plugins
● In D7, Views is objectoriented
● In D8, All of Drupal is objectoriented
# tutorial and documentation# Source: http://www.codem0nk3y.com/2012/04/whatbugsmeaboutmodxandwhy/cmslearningcurve/
Drupal 7 setup
# enable views and views_ui
drush en y views_ui
# enable the frontpage view at:
# admin/structure/views
# set 'Default front page' to 'frontpage' at:
# admin/config/system/siteinformation
# add some content
Drupal 7 setup
# Drupal 7# Mummy, where do views plugins come from?
# Look in s/a/m/contrib/views/includes/plugins.inc# All the 'native' views plugins are declared there.
/**
* Implements hook_views_plugins().
*/
function views_views_plugins() {
$plugins = array(
// display, style, row, argument default, argument validator and access.
● So we will also need to implement hook_views_plugins().
# drupal 7cd sites/all/modules/contrib/views/plugins && ls
class views_plugin_access_none extends views_plugin_accessclass views_plugin_access_perm extends views_plugin_accessclass views_plugin_access_role extends views_plugin_access
# using sublime text (ctrl p)# alternatively, use grep on the command line:
grep rin 'extends views_plugin' .
# Look in s/a/m/contrib/views/includes/plugins.inc# Views declares its own plugins by implementing
hook_views_plugins().
function views_views_plugins() {
$plugins = array(
// display, style, row, argument default, argument validator and access.
'access' => array(
'none' => array(
'title' => t('None'),
'help' => t('Will be available to all users.'),
'handler' => 'views_plugin_access_none',
'help topic' => 'accessnone',
),
# Let's create a D7 custom views access plugin
# Let's create a D7 custom views access plugin
mkdir p sites/all/modules/custom/my_views_plugins
cd sites/all/modules/custom/my_views_plugins
# Create a file named my_views_plugins.info:
name = My Views Plugins
description = Some views plugins examples.
core = 7.x
# Create an empty file named my_views_plugins.module.
# Enable our empty D7 module by browsing to:admin/modules
# In D7, we need to tell Drupal we are using views.# Copy the following text into my_views_plugins.module:
<?php
/**
* Implements hook_views_api().
*/
function my_views_plugin_views_api() {
return array(
'api' => '3',
);
}
# In D7, we need to implement hook_views_plugins().# Create the following file:
s/a/m/custom/my_views_plugins/my_views_plugin.views.inc
function my_views_plugin_views_plugins() {
$plugins = array();
// Tip: copy from: sites/all/modules/contrib/views/includes/plugins.inc
$plugins['access'] = array(
'time' => array(
'title' => t('Time'),
'help' => t('Access will be granted according to time of day.'),
'handler' => 'views_plugin_access_time',
),
);
return $plugins;
}
# We still need to tell D7 about the .inc file.# Add the files[] line to my_views_plugin.info:
name = My Views Plugins
description = Some views plugins examples.
core = 7.x
files[] = plugins/views_plugin_access_time.inc
# Clear the cache (drush cc views or admin/config/development/performance)
# Reload: admin/structure/views/view/frontpage/edit
# Now write the actual plugin class:# my_views_plugins/plugins/views_plugin_access_time.inc
class views_plugin_access_time extends views_plugin_access {
// Override the base class methods.
function access($account) {
return _my_views_plugin_access();
}
function get_access_callback() {
return array('_my_views_plugin_access');
}
# my_views_plugin.module
function _my_views_plugin_access() {
$date = new DateTime('now');
$hour = $date>format('H');
if ($hour > 22) {
// The children are in bed.
return TRUE;
}
return FALSE;
}
# This Demo is brought to you by Funny Cat
# Views Plugins in Drupal 8
mkdir p modules/custom/my_views_plugins
cd modules/custom/my_views_plugins
# Create a file named my_views_plugins.info.yml
name: My Views Plugins
type: module
description: Some views plugins examples.
core: 8.x
# Create an empty my_views_plugins.module file
<?php
# Enable our empty D8 module by browsing to:admin/modules
# In Drupal 8, views access plugins are to be found in a predictable location.
# This means that they can be easily discovered!
core/modules/user/src/Plugin/views/access/Permission.php
core/modules/user/src/Plugin/views/access/Role.php
core/modules/views/src/Plugin/views/access/None.php
● So, let's create the same directory structure …
(cd to your drupal8 root)
mkdir p modules/custom/my_views_plugins/src/Plugin/views/access
cd modules/custom/my_views_plugins/src/Plugin/views/access
● create a Time.php file here, copy None.php to start
# my_views_plugins/src/Plugin/views/access/Time.php
namespace Drupal\my_views_plugins\Plugin\views\access;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\Routing\Route;
use Drupal\views\Plugin\views\access\AccessPluginBase;
class Time extends AccessPluginBase {
public function summaryTitle() {}
public function access(AccountInterface $account) {}
public function alterRouteDefinition(Route $route) {}
}
# my_views_plugins/src/Plugin/views/access/Time.php# Implement all the methods of AccessPluginBase
class Time extends AccessPluginBase {
public function summaryTitle() {
return $this>t('Restricted to a time of day.');
}
public function access(AccountInterface $account) {
return _my_views_plugin_access();
}
public function alterRouteDefinition(Route $route) {
$route>setRequirement('_custom_access', '_my_views_plugin_access');
}
}
# my_views_plugins/my_views_plugins.module
function _my_views_plugin_access() {
$date = new \DateTime('now');
$hour = $date>format('H');
if ($hour >= 22) {
return \Drupal\Core\Access\AccessResult::allowed();
}
return \Drupal\Core\Access\AccessResult::forbidden();
}
# D8 Demo
Thank you!
Further learning:
● Larry Garfield
DrupalCon Amsterdam 2014: Drupal 8: The Crash Course
● Joe Shindelar
DrupalCon Amsterdam 2014: An Overview of the Drupal 8 Plugin System