SlideShare a Scribd company logo
1 of 30
Download to read offline
# Views Plugins in Drupal 7 and Drupal 8 
● Michael Lenahan, Developer at erdfisch 
● http://drupalcamp.berlin/program/sessions/views­plugins­d7­and­d8
# Key Points 
● In D7, views is extensible through plugins 
● In D8, all of Drupal is extensible through plugins 
● In D7, Views is object­oriented 
● In D8, All of Drupal is object­oriented
# tutorial and documentation 
# Source: http://www.codem0nk3y.com/2012/04/what­bugs­me­about­modx­and­why/ 
cms­learning­curve/
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/site­information 
# 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 7 
cd sites/all/modules/contrib/views/plugins && ls
class views_plugin_access_none extends views_plugin_access 
class views_plugin_access_perm extends views_plugin_access 
class 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' => 'access­none', 
),
# 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 drupal­8 
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 Drupalmy_views_pluginsPluginviewsaccess; 
use DrupalCoreSessionAccountInterface; 
use SymfonyComponentRoutingRoute; 
use DrupalviewsPluginviewsaccessAccessPluginBase; 
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 DrupalCoreAccessAccessResult::allowed(); 
} 
return DrupalCoreAccessAccessResult::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

More Related Content

What's hot

DevHub 3 - Composer plus Magento
DevHub 3 - Composer plus MagentoDevHub 3 - Composer plus Magento
DevHub 3 - Composer plus Magento
Magento Dev
 
Step by step guide for creating wordpress plugin
Step by step guide for creating wordpress pluginStep by step guide for creating wordpress plugin
Step by step guide for creating wordpress plugin
Mainak Goswami
 

What's hot (20)

DevHub 3 - Composer plus Magento
DevHub 3 - Composer plus MagentoDevHub 3 - Composer plus Magento
DevHub 3 - Composer plus Magento
 
Drupal Console Deep Dive: How to Develop Faster and Smarter on Drupal 8
Drupal Console Deep Dive: How to Develop Faster and Smarter on Drupal 8Drupal Console Deep Dive: How to Develop Faster and Smarter on Drupal 8
Drupal Console Deep Dive: How to Develop Faster and Smarter on Drupal 8
 
Dependency management in Magento with Composer
Dependency management in Magento with ComposerDependency management in Magento with Composer
Dependency management in Magento with Composer
 
Wordpress development: A Modern Approach
Wordpress development:  A Modern ApproachWordpress development:  A Modern Approach
Wordpress development: A Modern Approach
 
An Introduction to Drupal
An Introduction to DrupalAn Introduction to Drupal
An Introduction to Drupal
 
Voiture tech talk
Voiture tech talkVoiture tech talk
Voiture tech talk
 
How to develope plugin in wordpress: 6 simple steps.
How to develope plugin in wordpress: 6 simple steps.How to develope plugin in wordpress: 6 simple steps.
How to develope plugin in wordpress: 6 simple steps.
 
Unlocked package
Unlocked packageUnlocked package
Unlocked package
 
Wordpress Plugin Development Short Tutorial
Wordpress Plugin Development Short TutorialWordpress Plugin Development Short Tutorial
Wordpress Plugin Development Short Tutorial
 
WordPress Realtime - WordCamp São Paulo 2015
WordPress Realtime - WordCamp São Paulo 2015WordPress Realtime - WordCamp São Paulo 2015
WordPress Realtime - WordCamp São Paulo 2015
 
Behaviour Driven Development con Behat & Drupal
Behaviour Driven Development con Behat & DrupalBehaviour Driven Development con Behat & Drupal
Behaviour Driven Development con Behat & Drupal
 
Backbone.js
Backbone.jsBackbone.js
Backbone.js
 
Providing the ultimate publishing experience
Providing the ultimate publishing experienceProviding the ultimate publishing experience
Providing the ultimate publishing experience
 
Pde build
Pde buildPde build
Pde build
 
wp-cli
wp-cliwp-cli
wp-cli
 
JavaScript Dependencies, Modules & Browserify
JavaScript Dependencies, Modules & BrowserifyJavaScript Dependencies, Modules & Browserify
JavaScript Dependencies, Modules & Browserify
 
Packing it all: JavaScript module bundling from 2000 to now
Packing it all: JavaScript module bundling from 2000 to nowPacking it all: JavaScript module bundling from 2000 to now
Packing it all: JavaScript module bundling from 2000 to now
 
Step by step guide for creating wordpress plugin
Step by step guide for creating wordpress pluginStep by step guide for creating wordpress plugin
Step by step guide for creating wordpress plugin
 
FRONT-END COMPONENTS IN DRUPAL THEME. "KAIZEN" - DRUPAL 8 THEME FROM SKILLD
FRONT-END COMPONENTS IN DRUPAL THEME. "KAIZEN" - DRUPAL 8 THEME FROM SKILLDFRONT-END COMPONENTS IN DRUPAL THEME. "KAIZEN" - DRUPAL 8 THEME FROM SKILLD
FRONT-END COMPONENTS IN DRUPAL THEME. "KAIZEN" - DRUPAL 8 THEME FROM SKILLD
 
Extending MadCap Flare HTML5 Targets with jQuery - MadWorld 2016, Scott DeLoa...
Extending MadCap Flare HTML5 Targets with jQuery - MadWorld 2016, Scott DeLoa...Extending MadCap Flare HTML5 Targets with jQuery - MadWorld 2016, Scott DeLoa...
Extending MadCap Flare HTML5 Targets with jQuery - MadWorld 2016, Scott DeLoa...
 

Similar to Views plugins-in-d7-and-d8

Automating Drupal Development: Makefiles, features and beyond
Automating Drupal Development: Makefiles, features and beyondAutomating Drupal Development: Makefiles, features and beyond
Automating Drupal Development: Makefiles, features and beyond
Nuvole
 
Write your first WordPress plugin
Write your first WordPress pluginWrite your first WordPress plugin
Write your first WordPress plugin
Anthony Montalbano
 

Similar to Views plugins-in-d7-and-d8 (20)

Building and Maintaining a Distribution in Drupal 7 with Features
Building and Maintaining a  Distribution in Drupal 7 with FeaturesBuilding and Maintaining a  Distribution in Drupal 7 with Features
Building and Maintaining a Distribution in Drupal 7 with Features
 
Drupal Day 2012 - Automating Drupal Development: Make!les, Features and Beyond
Drupal Day 2012 - Automating Drupal Development: Make!les, Features and BeyondDrupal Day 2012 - Automating Drupal Development: Make!les, Features and Beyond
Drupal Day 2012 - Automating Drupal Development: Make!les, Features and Beyond
 
Drupal distributions - how to build them
Drupal distributions - how to build themDrupal distributions - how to build them
Drupal distributions - how to build them
 
Drupal 8 - Core and API Changes
Drupal 8 - Core and API ChangesDrupal 8 - Core and API Changes
Drupal 8 - Core and API Changes
 
Drupal 8 improvements for developer productivity php symfony and more
Drupal 8 improvements for developer productivity  php symfony and moreDrupal 8 improvements for developer productivity  php symfony and more
Drupal 8 improvements for developer productivity php symfony and more
 
Decoupling Drupal mit dem Lupus Nuxt.js Drupal Stack
Decoupling Drupal mit dem Lupus Nuxt.js Drupal StackDecoupling Drupal mit dem Lupus Nuxt.js Drupal Stack
Decoupling Drupal mit dem Lupus Nuxt.js Drupal Stack
 
Using Composer with Drupal and Drush
Using Composer with Drupal and DrushUsing Composer with Drupal and Drush
Using Composer with Drupal and Drush
 
Automating Drupal Development: Makefiles, features and beyond
Automating Drupal Development: Makefiles, features and beyondAutomating Drupal Development: Makefiles, features and beyond
Automating Drupal Development: Makefiles, features and beyond
 
Drupal 8 simple page: Mi primer proyecto en Drupal 8.
Drupal 8 simple page: Mi primer proyecto en Drupal 8.Drupal 8 simple page: Mi primer proyecto en Drupal 8.
Drupal 8 simple page: Mi primer proyecto en Drupal 8.
 
Composer Tools & Frameworks for Drupal
Composer Tools & Frameworks for DrupalComposer Tools & Frameworks for Drupal
Composer Tools & Frameworks for Drupal
 
Introduction And Basics of Modules in Drupal 7
Introduction And Basics of Modules in Drupal 7 Introduction And Basics of Modules in Drupal 7
Introduction And Basics of Modules in Drupal 7
 
DDAY2014 - Features per Drupal 8
DDAY2014 - Features per Drupal 8DDAY2014 - Features per Drupal 8
DDAY2014 - Features per Drupal 8
 
Welcome aboard the team
Welcome aboard the teamWelcome aboard the team
Welcome aboard the team
 
Simple module Development in Joomla! 2.5
Simple module Development in Joomla! 2.5Simple module Development in Joomla! 2.5
Simple module Development in Joomla! 2.5
 
Write your first WordPress plugin
Write your first WordPress pluginWrite your first WordPress plugin
Write your first WordPress plugin
 
Using Drupal Features in B-Translator
Using Drupal Features in B-TranslatorUsing Drupal Features in B-Translator
Using Drupal Features in B-Translator
 
Advanced WordPress Optimization - iGaming Supershow 2012
Advanced WordPress Optimization - iGaming Supershow 2012Advanced WordPress Optimization - iGaming Supershow 2012
Advanced WordPress Optimization - iGaming Supershow 2012
 
13th Sep, Drupal 7 advanced training by TCS
13th Sep, Drupal 7 advanced training by TCS 13th Sep, Drupal 7 advanced training by TCS
13th Sep, Drupal 7 advanced training by TCS
 
Wordpress as a framework
Wordpress as a frameworkWordpress as a framework
Wordpress as a framework
 
Towards the perfect Drupal Dev Machine
Towards the perfect Drupal Dev MachineTowards the perfect Drupal Dev Machine
Towards the perfect Drupal Dev Machine
 

Recently uploaded

一比一原版(Offer)康考迪亚大学毕业证学位证靠谱定制
一比一原版(Offer)康考迪亚大学毕业证学位证靠谱定制一比一原版(Offer)康考迪亚大学毕业证学位证靠谱定制
一比一原版(Offer)康考迪亚大学毕业证学位证靠谱定制
pxcywzqs
 
一比一原版英国格林多大学毕业证如何办理
一比一原版英国格林多大学毕业证如何办理一比一原版英国格林多大学毕业证如何办理
一比一原版英国格林多大学毕业证如何办理
AS
 
一比一原版美国北卡罗莱纳大学毕业证如何办理
一比一原版美国北卡罗莱纳大学毕业证如何办理一比一原版美国北卡罗莱纳大学毕业证如何办理
一比一原版美国北卡罗莱纳大学毕业证如何办理
A
 
Abortion Clinic in Germiston +27791653574 WhatsApp Abortion Clinic Services i...
Abortion Clinic in Germiston +27791653574 WhatsApp Abortion Clinic Services i...Abortion Clinic in Germiston +27791653574 WhatsApp Abortion Clinic Services i...
Abortion Clinic in Germiston +27791653574 WhatsApp Abortion Clinic Services i...
mikehavy0
 
一比一原版奥兹学院毕业证如何办理
一比一原版奥兹学院毕业证如何办理一比一原版奥兹学院毕业证如何办理
一比一原版奥兹学院毕业证如何办理
F
 
一比一原版(毕业证书)新西兰怀特克利夫艺术设计学院毕业证原件一模一样
一比一原版(毕业证书)新西兰怀特克利夫艺术设计学院毕业证原件一模一样一比一原版(毕业证书)新西兰怀特克利夫艺术设计学院毕业证原件一模一样
一比一原版(毕业证书)新西兰怀特克利夫艺术设计学院毕业证原件一模一样
AS
 
一比一原版贝德福特大学毕业证学位证书
一比一原版贝德福特大学毕业证学位证书一比一原版贝德福特大学毕业证学位证书
一比一原版贝德福特大学毕业证学位证书
F
 
一比一原版(Polytechnic毕业证书)新加坡理工学院毕业证原件一模一样
一比一原版(Polytechnic毕业证书)新加坡理工学院毕业证原件一模一样一比一原版(Polytechnic毕业证书)新加坡理工学院毕业证原件一模一样
一比一原版(Polytechnic毕业证书)新加坡理工学院毕业证原件一模一样
AS
 
一比一原版犹他大学毕业证如何办理
一比一原版犹他大学毕业证如何办理一比一原版犹他大学毕业证如何办理
一比一原版犹他大学毕业证如何办理
F
 
一比一原版(NYU毕业证书)美国纽约大学毕业证学位证书
一比一原版(NYU毕业证书)美国纽约大学毕业证学位证书一比一原版(NYU毕业证书)美国纽约大学毕业证学位证书
一比一原版(NYU毕业证书)美国纽约大学毕业证学位证书
c6eb683559b3
 
一比一原版桑佛德大学毕业证成绩单申请学校Offer快速办理
一比一原版桑佛德大学毕业证成绩单申请学校Offer快速办理一比一原版桑佛德大学毕业证成绩单申请学校Offer快速办理
一比一原版桑佛德大学毕业证成绩单申请学校Offer快速办理
apekaom
 
一比一原版(Dundee毕业证书)英国爱丁堡龙比亚大学毕业证如何办理
一比一原版(Dundee毕业证书)英国爱丁堡龙比亚大学毕业证如何办理一比一原版(Dundee毕业证书)英国爱丁堡龙比亚大学毕业证如何办理
一比一原版(Dundee毕业证书)英国爱丁堡龙比亚大学毕业证如何办理
AS
 

Recently uploaded (20)

一比一原版(Offer)康考迪亚大学毕业证学位证靠谱定制
一比一原版(Offer)康考迪亚大学毕业证学位证靠谱定制一比一原版(Offer)康考迪亚大学毕业证学位证靠谱定制
一比一原版(Offer)康考迪亚大学毕业证学位证靠谱定制
 
Meaning of On page SEO & its process in detail.
Meaning of On page SEO & its process in detail.Meaning of On page SEO & its process in detail.
Meaning of On page SEO & its process in detail.
 
一比一原版英国格林多大学毕业证如何办理
一比一原版英国格林多大学毕业证如何办理一比一原版英国格林多大学毕业证如何办理
一比一原版英国格林多大学毕业证如何办理
 
[Hackersuli] Élő szövet a fémvázon: Python és gépi tanulás a Zeek platformon
[Hackersuli] Élő szövet a fémvázon: Python és gépi tanulás a Zeek platformon[Hackersuli] Élő szövet a fémvázon: Python és gépi tanulás a Zeek platformon
[Hackersuli] Élő szövet a fémvázon: Python és gépi tanulás a Zeek platformon
 
一比一原版美国北卡罗莱纳大学毕业证如何办理
一比一原版美国北卡罗莱纳大学毕业证如何办理一比一原版美国北卡罗莱纳大学毕业证如何办理
一比一原版美国北卡罗莱纳大学毕业证如何办理
 
APNIC Policy Roundup presented by Sunny Chendi at TWNOG 5.0
APNIC Policy Roundup presented by Sunny Chendi at TWNOG 5.0APNIC Policy Roundup presented by Sunny Chendi at TWNOG 5.0
APNIC Policy Roundup presented by Sunny Chendi at TWNOG 5.0
 
Abortion Clinic in Germiston +27791653574 WhatsApp Abortion Clinic Services i...
Abortion Clinic in Germiston +27791653574 WhatsApp Abortion Clinic Services i...Abortion Clinic in Germiston +27791653574 WhatsApp Abortion Clinic Services i...
Abortion Clinic in Germiston +27791653574 WhatsApp Abortion Clinic Services i...
 
一比一原版奥兹学院毕业证如何办理
一比一原版奥兹学院毕业证如何办理一比一原版奥兹学院毕业证如何办理
一比一原版奥兹学院毕业证如何办理
 
一比一原版(毕业证书)新西兰怀特克利夫艺术设计学院毕业证原件一模一样
一比一原版(毕业证书)新西兰怀特克利夫艺术设计学院毕业证原件一模一样一比一原版(毕业证书)新西兰怀特克利夫艺术设计学院毕业证原件一模一样
一比一原版(毕业证书)新西兰怀特克利夫艺术设计学院毕业证原件一模一样
 
一比一原版贝德福特大学毕业证学位证书
一比一原版贝德福特大学毕业证学位证书一比一原版贝德福特大学毕业证学位证书
一比一原版贝德福特大学毕业证学位证书
 
Lowongan Kerja LC Yogyakarta Terbaru 085746015303
Lowongan Kerja LC Yogyakarta Terbaru 085746015303Lowongan Kerja LC Yogyakarta Terbaru 085746015303
Lowongan Kerja LC Yogyakarta Terbaru 085746015303
 
APNIC Policy Roundup, presented by Sunny Chendi at the 5th ICANN APAC-TWNIC E...
APNIC Policy Roundup, presented by Sunny Chendi at the 5th ICANN APAC-TWNIC E...APNIC Policy Roundup, presented by Sunny Chendi at the 5th ICANN APAC-TWNIC E...
APNIC Policy Roundup, presented by Sunny Chendi at the 5th ICANN APAC-TWNIC E...
 
一比一原版(Polytechnic毕业证书)新加坡理工学院毕业证原件一模一样
一比一原版(Polytechnic毕业证书)新加坡理工学院毕业证原件一模一样一比一原版(Polytechnic毕业证书)新加坡理工学院毕业证原件一模一样
一比一原版(Polytechnic毕业证书)新加坡理工学院毕业证原件一模一样
 
一比一原版犹他大学毕业证如何办理
一比一原版犹他大学毕业证如何办理一比一原版犹他大学毕业证如何办理
一比一原版犹他大学毕业证如何办理
 
一比一原版(NYU毕业证书)美国纽约大学毕业证学位证书
一比一原版(NYU毕业证书)美国纽约大学毕业证学位证书一比一原版(NYU毕业证书)美国纽约大学毕业证学位证书
一比一原版(NYU毕业证书)美国纽约大学毕业证学位证书
 
一比一原版桑佛德大学毕业证成绩单申请学校Offer快速办理
一比一原版桑佛德大学毕业证成绩单申请学校Offer快速办理一比一原版桑佛德大学毕业证成绩单申请学校Offer快速办理
一比一原版桑佛德大学毕业证成绩单申请学校Offer快速办理
 
一比一原版(Dundee毕业证书)英国爱丁堡龙比亚大学毕业证如何办理
一比一原版(Dundee毕业证书)英国爱丁堡龙比亚大学毕业证如何办理一比一原版(Dundee毕业证书)英国爱丁堡龙比亚大学毕业证如何办理
一比一原版(Dundee毕业证书)英国爱丁堡龙比亚大学毕业证如何办理
 
APNIC Updates presented by Paul Wilson at CaribNOG 27
APNIC Updates presented by Paul Wilson at  CaribNOG 27APNIC Updates presented by Paul Wilson at  CaribNOG 27
APNIC Updates presented by Paul Wilson at CaribNOG 27
 
20240507 QFM013 Machine Intelligence Reading List April 2024.pdf
20240507 QFM013 Machine Intelligence Reading List April 2024.pdf20240507 QFM013 Machine Intelligence Reading List April 2024.pdf
20240507 QFM013 Machine Intelligence Reading List April 2024.pdf
 
Loker Pemandu Lagu LC Semarang 085746015303
Loker Pemandu Lagu LC Semarang 085746015303Loker Pemandu Lagu LC Semarang 085746015303
Loker Pemandu Lagu LC Semarang 085746015303
 

Views plugins-in-d7-and-d8

  • 1. # Views Plugins in Drupal 7 and Drupal 8 ● Michael Lenahan, Developer at erdfisch ● http://drupalcamp.berlin/program/sessions/views­plugins­d7­and­d8
  • 2. # Key Points ● In D7, views is extensible through plugins ● In D8, all of Drupal is extensible through plugins ● In D7, Views is object­oriented ● In D8, All of Drupal is object­oriented
  • 3. # tutorial and documentation # Source: http://www.codem0nk3y.com/2012/04/what­bugs­me­about­modx­and­why/ cms­learning­curve/
  • 4. 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/site­information # add some content
  • 6. # Drupal 7 # Mummy, where do views plugins come from?
  • 7. # 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().
  • 8. # drupal 7 cd sites/all/modules/contrib/views/plugins && ls
  • 9. class views_plugin_access_none extends views_plugin_access class views_plugin_access_perm extends views_plugin_access class views_plugin_access_role extends views_plugin_access
  • 10. # using sublime text (ctrl p) # alternatively, use grep on the command line: grep ­rin 'extends views_plugin' .
  • 11. # 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' => 'access­none', ),
  • 12. # Let's create a D7 custom views access plugin
  • 13. # 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.
  • 14. # Enable our empty D7 module by browsing to: admin/modules
  • 15. # 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', ); }
  • 16. # 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; }
  • 17. # 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
  • 18. # Clear the cache (drush cc views or admin/config/development/performance) # Reload: admin/structure/views/view/frontpage/edit
  • 19.
  • 20. # 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'); }
  • 21. # 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; }
  • 22. # This Demo is brought to you by Funny Cat
  • 23. # 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
  • 24. # Enable our empty D8 module by browsing to: admin/modules
  • 25. # 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 drupal­8 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
  • 26. # my_views_plugins/src/Plugin/views/access/Time.php namespace Drupalmy_views_pluginsPluginviewsaccess; use DrupalCoreSessionAccountInterface; use SymfonyComponentRoutingRoute; use DrupalviewsPluginviewsaccessAccessPluginBase; class Time extends AccessPluginBase { public function summaryTitle() {} public function access(AccountInterface $account) {} public function alterRouteDefinition(Route $route) {} }
  • 27. # 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'); } }
  • 28. # my_views_plugins/my_views_plugins.module function _my_views_plugin_access() { $date = new DateTime('now'); $hour = $date­> format('H'); if ($hour >= 22) { return DrupalCoreAccessAccessResult::allowed(); } return DrupalCoreAccessAccessResult::forbidden(); }
  • 30. 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