Upgrading Drupal Modules - php[world] 2015

Chris Tankersley
Chris TankersleyPHP Programmer at Home
Upgrading Drupal
Modules
Chris	
  Tankersley	
  
php[world]	
  2015	
  
php[world]	
  2015	
   1	
  
Who Am I
•  PHP	
  Programmer	
  for	
  over	
  10	
  years	
  
•  Drupal	
  Developer	
  for	
  5	
  Years	
  
•  Symfony	
  2,	
  Silex,	
  and	
  ZF2	
  
Programmer	
  
•  Maintainer	
  of	
  Social	
  Media	
  Bar	
  
•  hJps://github.com/dragonmantank	
  
php[world]	
  2015	
   2	
  
It’s Finally Happening!!!!1!!one
php[world]	
  2015	
   3	
  
Now we have to update our modules
php[world]	
  2015	
   4	
  
In the Before Times
php[world]	
  2015	
   5	
  
Drupal 6 to Drupal 7
php[world]	
  2015	
   6	
  
Drupal	
  6	
   Drupal	
  7	
  
Fix	
  All	
  the	
  API	
  Changes	
  
Drupal 7 to Drupal 8
php[world]	
  2015	
   7	
  
Drupal	
  7	
  
Drupal 7 to Drupal 8
php[world]	
  2015	
   8	
  
Drupal	
  7	
  
PreJy	
  Much	
  Just	
  API	
  Changes	
  
Social Media Bar
php[world]	
  2015	
   9	
  
Gotta Share Them all
php[world]	
  2015	
   10	
  
Client Project
php[world]	
  2015	
   11	
  
Open Source and Give Back
php[world]	
  2015	
   12	
  
Gotta Update Them All
php[world]	
  2015	
   13	
  
Updating the Module
php[world]	
  2015	
   14	
  
New Structure
modules/
|
‘- SocialMediaBar
+ src/
+ SocialMediaBar.info.yml
+ SocialMediaBar.routing.yml
php[world]	
  2015	
   15	
  
Big Changes
•  PSR4	
  Autoloading	
  (yay!)	
  
•  YAML	
  config	
  files	
  instead	
  of	
  INI	
  config	
  files	
  
•  Real	
  objects	
  for	
  nearly	
  everything	
  
•  Much	
  cleaner	
  separa`on	
  of	
  concerns	
  for	
  code	
  
php[world]	
  2015	
   16	
  
Good News
Much	
  of	
  your	
  business	
  logic	
  will	
  
probably	
  be	
  copy-­‐paste	
  work	
  
php[world]	
  2015	
   17	
  
Bad News
Much	
  of	
  the	
  documenta`on	
  is	
  
s`ll	
  out	
  of	
  date	
  
php[world]	
  2015	
   18	
  
Let’s Get Started
php[world]	
  2015	
   19	
  
New Structure
modules/
|
‘- SocialMediaBar
+ src/
+ SocialMediaBar.info.yml
+ SocialMediaBar.routing.yml
php[world]	
  2015	
   20	
  
Move your module?
•  Core	
  modules	
  live	
  in	
  core/modules	
  now	
  
•  modules/	
  isn’t	
  off	
  limits	
  anymore!	
  
•  Can	
  s`ll	
  live	
  in	
  sites/*/modules/
•  Naming	
  conven`ons	
  s`ll	
  apply	
  
php[world]	
  2015	
   21	
  
Info Files and YAML
php[world]	
  2015	
   22	
  
New Structure
modules/
|
‘- SocialMediaBar
+ src/
+ SocialMediaBar.info.yml
+ SocialMediaBar.routing.yml
php[world]	
  2015	
   23	
  
YAML Format
•  Human	
  and	
  machine	
  readable	
  markup	
  language	
  
•  Indenta`on-­‐based	
  markup	
  
•  Supports	
  most	
  basic	
  types	
  of	
  data	
  
•  Drupal	
  uses	
  it	
  for	
  module,	
  theme,	
  and	
  configura`on	
  informa`on	
  
php[world]	
  2015	
   24	
  
Update Your .info File
•  We	
  are	
  switching	
  from	
  .ini	
  format	
  to	
  .yml	
  format	
  
•  Modules	
  must	
  add	
  type: module	
  to	
  actually	
  show	
  up	
  as	
  a	
  module	
  
php[world]	
  2015	
   25	
  
socialmediabar.info
name = Social Media Bar
description = Adds a social media bar to different content types
core = 7.x
package = "Social Media"
php = 5.3
stylesheets[all][] = css/socialmediabar.css
scripts[] = js/socialmediabar.js
dependencies[] = libraries
configure = admin/config/services/socialmediabar
php[world]	
  2015	
   26	
  
socialmediabar.info.yml
name: Social Media Bar
description: Adds a social media bar to different content types
core: 8.x
type: module
package: "Social Media"
php[world]	
  2015	
   27	
  
New Routing System
php[world]	
  2015	
   28	
  
New Structure
modules/
|
‘- SocialMediaBar
+ src/
+ SocialMediaBar.info.yml
+ SocialMediaBar.routing.yml
php[world]	
  2015	
   29	
  
hook_menu() is dead, long live Routing!
•  This	
  is	
  all	
  moved	
  to	
  YAML	
  files	
  
•  hook_menu()	
  is	
  now	
  split	
  into	
  routing.yml	
  and	
  links.*.yml
•  routing.yml	
  handles	
  defining	
  URIs	
  and	
  route	
  config	
  
•  links.*.yml	
  handles	
  how	
  links	
  appear	
  in	
  the	
  system	
  
php[world]	
  2015	
   30	
  
socialmediabar_menu()
php[world]	
  2015	
   31	
  
function socialmediabar_menu() {
return array(
'admin/config/services/socialmediabar' => array(
'title' => 'Social Media Bar Admin',
'description' => 'Set up configuration options for the Social Media Bar',
'page callback' => 'drupal_get_form',
'page arguments' => array('socialmediabar_admin_form'),
'access arguments' => array('administer socialmediabar settings'),
),
'socialmediabar/countproxy' => array(
'title' => 'Social Media Bar Admin',
'page callback' => 'socialmediabar_countproxy',
'type' => MENU_CALLBACK,
'access arguments' => array('access content'),
),
'socialmediabar/shareproxy' => array(
'page callback' => 'socialmediabar_shareproxy',
'type' => MENU_CALLBACK,
'access arguments' => array('access content'),
),
);
}
socialmediabar.routing.yml
php[world]	
  2015	
  
socialmediabar.admin:
path: "/admin/config/services/SocialMediaBar"
defaults:
_form: "DrupalSocialMediaBarFormAdminForm"
_title: "Social Media Bar Admin"
requirements:
_permission: "access content"
socialmediabar.count_proxy:
path: "/socialmediabar/countproxy"
defaults:
_controller: "DrupalSocialMediaBarController
CountProxyController::index"
requirements:
_access: 'TRUE’
32	
  
socialmediabar.links.menu.yml
php[world]	
  2015	
   33	
  
socialmediabar.admin:
title: 'Social Media Bar Admin'
parent: system.admin_config_services
description: 'Configure the Social Media Bar sharing bar'
route_name: socialmediabar.admin
PSR-4 Autoloading
php[world]	
  2015	
   34	
  
What is PSR-4
•  Defines	
  a	
  folder	
  structure	
  so	
  that	
  classes	
  can	
  be	
  loaded	
  on	
  demand,	
  
instead	
  of	
  all	
  at	
  once	
  
•  Unlike	
  PSR-­‐0,	
  allows	
  shallow	
  and	
  split	
  folders	
  for	
  loading	
  
•  Much	
  more	
  efficient	
  than	
  the	
  the	
  old	
  files[]	
  key	
  in	
  .info
•  No	
  more	
  require	
  at	
  the	
  top	
  of	
  your	
  files	
  
•  All	
  of	
  your	
  code	
  will	
  now	
  live	
  in	
  src/
•  All	
  of	
  your	
  code	
  will	
  now	
  properly	
  namespaced	
  
php[world]	
  2015	
   35	
  
Drupal 8 is turtles objects all the way down
php[world]	
  2015	
   36	
  
hJps://upload.wikimedia.org/wikipedia/commons/4/47/River_terrapin.jpg	
  
What is Namespacing?
•  In	
  PHP,	
  a	
  way	
  to	
  separate	
  named	
  classes	
  and	
  ‘bundle’	
  them	
  together	
  
•  Allows	
  for	
  renaming	
  of	
  classes	
  to	
  avoid	
  class	
  name	
  duplica`on	
  
namespace DrupalSocialMediaBarController;
use DrupalCoreFormConfigFormBase;
use DrupalCoreControllerControllerBase as Base;
php[world]	
  2015	
   37	
  
Your Module’s Namespace
Drupal[modulename]
Lives	
  in	
  
modules/[modulename]/src
sites/all/modules/[modulename]/src
sites/default/modules/[modulename]/src
php[world]	
  2015	
   38	
  
Why is it under the Drupal namespace?
php[world]	
  2015	
   39	
  
Form Objects
php[world]	
  2015	
   40	
  
socialmediabar.routing.yml
php[world]	
  2015	
  
socialmediabar.admin:
path: "/admin/config/services/SocialMediaBar"
defaults:
_form: "DrupalSocialMediaBarFormAdminForm"
_title: "Social Media Bar Admin"
requirements:
_permission: "access content"
socialmediabar.count_proxy:
path: "/socialmediabar/countproxy"
defaults:
_controller: "DrupalSocialMediaBarController
CountProxyController::index"
requirements:
_access: 'TRUE’
41	
  
New Structure
modules/
|
‘- SocialMediaBar
+ src/
+ Form/
+ AdminForm.php
+ SocialMediaBar.info.yml
+ SocialMediaBar.routing.yml
php[world]	
  2015	
   42	
  
Form API Changes
•  The	
  overall	
  syntax	
  is	
  s`ll	
  the	
  same	
  
•  Form	
  State	
  is	
  now	
  an	
  objects	
  instead	
  of	
  an	
  array	
  
•  Routes	
  now	
  call	
  forms	
  using	
  the	
  _form	
  key	
  instead	
  of	
  
drupal_get_form()
•  Forms	
  are	
  now	
  Objects,	
  extending	
  DrupalCoreFormFormBase
•  System	
  forms	
  now	
  extend DrupalCoreFormConfigFormBase
php[world]	
  2015	
   43	
  
System/Config Forms
php[world]	
  2015	
   44	
  
namespace DrupalSocialMediaBarForm;
use DrupalCoreFormConfigFormBase;
use DrupalCoreFormFormStateInterface;
class AdminForm extends ConfigFormBase {
public function getEditableConfigNames()
public function getFormId()
public function buildForm(array $form, FormStateInterface $form_state)
public function submitForm(array &$form, FormStateInterface $form_state)
}
Configuration
php[world]	
  2015	
   45	
  
Configuration is now Object-Based
•  Injected	
  into	
  Controllers	
  and	
  forms	
  
•  Can	
  be	
  accessed	
  by	
  Drupal::config()	
  if	
  needed	
  
•  Handled	
  by	
  namespaced	
  areas	
  of	
  configura`on	
  
•  socialmediabar.config
•  Get	
  	
  individual	
  keys	
  from	
  the	
  config,	
  set	
  them	
  to	
  change	
  values,	
  and	
  
save	
  them	
  to	
  commit	
  the	
  changes	
  
php[world]	
  2015	
   46	
  
php[world]	
  2015	
   47	
  
public function buildForm(array $form, FormStateInterface $form_state)
{
$config = $this->config('socialmediabar.config');
$enabled_networks = $config->get('socialmediabar_enabled_networks') ?: [];
// ...
$form['available_networks']['socialmediabar_enabled_networks'] = array(
'#type' => 'checkboxes',
'#options' => array(
'facebook' => t('Facebook'),
'twitter' => t('Twitter'),
'linkedin' => t('LinkedIn'),
'googleplus' => t('Google+'),
'email' => t('ShareThis Email'),
),
'#default_value' => array_values($enabled_networks),
);
// ...
}
php[world]	
  2015	
   48	
  
public function submitForm(array &$form, FormStateInterface $form_state)
{
parent::submitForm($form, $form_state);
$enabled_networks = $form_state->getValue('socialmediabar_enabled_networks');
$cache_time = $form_state->getValue('socialmediabar_cache_time’);
$api_key = $form_state->getValue('socialmediabar_sharethis_apikey');
$config = $this->configFactory->getEditable('socialmediabar.config');
$config
->set('socialmediabar_enabled_networks’, $enabled_networks)
->set('socialmediabar_cache_time',$cache_time)
->set('socialmediabar_sharethis_apikey’, $api_key)
->save()
;
}
Controllers
php[world]	
  2015	
   49	
  
socialmediabar.routing.yml
php[world]	
  2015	
  
socialmediabar.admin:
path: "/admin/config/services/SocialMediaBar"
defaults:
_form: "DrupalSocialMediaBarFormAdminForm"
_title: "Social Media Bar Admin"
requirements:
_permission: "access content"
socialmediabar.count_proxy:
path: "/socialmediabar/countproxy"
defaults:
_controller: "DrupalSocialMediaBarControllerCountProxyController::index"
requirements:
_access: 'TRUE’
50	
  
New Structure
modules/
|
‘- SocialMediaBar
+ src/
+ Controller/
+ CountProxyController.php
+ SocialMediaBar.info.yml
+ SocialMediaBar.routing.yml
php[world]	
  2015	
   51	
  
No More Callback Functions, Use Objects
•  MENU_CALLBACK	
  func`ons	
  are	
  now	
  object::method	
  signatures	
  
•  Controllers	
  extend	
  DrupalCoreControllerControllerBase
•  View	
  output	
  is	
  now	
  handled	
  by	
  Response	
  Objects	
  
php[world]	
  2015	
   52	
  
php[world]	
  2015	
   53	
  
namespace DrupalSocialMediaBarController;
use DrupalCoreControllerControllerBase;
use DrupalSocialMediaBarNetworkNetworkFactory;
use SymfonyComponentHttpFoundationJsonResponse;
class CountProxyController extends ControllerBase {
public function index() {
$url = Drupal::request()->get('url');
$provider = Drupal::request()->get('provider');
$errors = [];
// ...
if (count($errors)) {
return new JsonResponse(['errors' => $errors], 400);
}
try {
$network = NetworkFactory::getNetwork($provider);
$count = $network->getCount($url);
return new JsonResponse([$provider => ['outbound' => $count]]);
} catch (Exception $e) {
return new JsonResponse(['errors' => [$e->getMessage()]], 500);
}
}
}
Theming
php[world]	
  2015	
   54	
  
New Structure
modules/
|
‘- SocialMediaBar
+ src/
+ templates/
+ socialmediabar.html.twig
+ SocialMediaBar.info.yml
+ SocialMediaBar.routing.yml
php[world]	
  2015	
   55	
  
hook_theme(), booooooooooooo!
•  Has	
  to	
  live	
  in	
  [modulename].module	
  
•  Automa`cally	
  looks	
  in	
  the	
  templates/ folder	
  for	
  Twig	
  files	
  
php[world]	
  2015	
   56	
  
function SocialMediaBar_theme() {
return array(
'socialmediabar' => array(
'template' => 'socialmediabar',
'variables' => array('api_key' => NULL, 'networks' => NULL),
),
);
}
Things are attached via #attached
•  No	
  longer	
  have	
  drupal_add_css() or	
  drupal_add_js()
•  Can	
  easily	
  bundle	
  up	
  CSS	
  and	
  JS	
  into	
  ‘libraries’	
  
•  [modulename].libraries.yml	
  
•  #attached	
  can	
  be	
  used	
  with	
  a	
  render	
  array,	
  or	
  form	
  descrip`ons	
  
•  hJps://www.drupal.org/developing/api/8/assets	
  
php[world]	
  2015	
   57	
  
JavaScript settings changes
•  Now	
  called	
  drupalSettings	
  instead	
  of	
  Drupal.settings
•  Passed	
  via	
  a	
  drupalSettings	
  key	
  on	
  #attached
php[world]	
  2015	
   58	
  
php[world]	
  2015	
   59	
  
public function render() {
$config = Drupal::config('socialmediabar.config');
$drupalSettings = [
'apiKey' => $config->get('socialmediabar_sharethis_apikey'),
];
$networks = $config->get('socialmediabar_enabled_networks');
$response = [
'#theme' => 'socialmediabar',
'#api_key' => $drupalSettings['apiKey'],
'#networks' => $networks,
'#attached' => [
'library' => ['SocialMediaBar/socialmediabar'],
'drupalSettings' => ['socialmediabar' => $drupalSettings],
],
];
return render($response);
}
Twig Extensions
php[world]	
  2015	
   60	
  
Twig Extensions
•  Allows	
  func`on	
  calls	
  inside	
  of	
  a	
  Twig	
  file	
  
php[world]	
  2015	
   61	
  
socialmediabar.services.yml
php[world]	
  2015	
   62	
  
services:
socialmediabar.twig.renderer:
class: DrupalSocialMediaBarTwigRendererExtension
tags:
- { name: twig.extension }
php[world]	
  2015	
   63	
  
namespace DrupalSocialMediaBarTwig;
class RendererExtension extends Twig_Extension
{
public function getName() {
return 'socialmediabar.renderer';
}
public function getFunctions() {
return [
new Twig_SimpleFunction('socialmediabar_render', [$this, 'render'], [
'is_safe' => [true],
])
];
}
public function render() {
// ...
}
}
Thank You!
hJp://ctankersley.com	
  
chris@ctankersley.com	
  
@dragonmantank	
  
	
  
hJps://joind.in/14800	
  
php[world]	
  2015	
   64	
  
1 of 64

Recommended

WordPress REST API hacking by
WordPress REST API hackingWordPress REST API hacking
WordPress REST API hackingJeroen van Dijk
2K views56 slides
php[world] 2016 - You Don’t Need Node.js - Async Programming in PHP by
php[world] 2016 - You Don’t Need Node.js - Async Programming in PHPphp[world] 2016 - You Don’t Need Node.js - Async Programming in PHP
php[world] 2016 - You Don’t Need Node.js - Async Programming in PHPAdam Englander
496 views31 slides
Phone calls and sms from php by
Phone calls and sms from phpPhone calls and sms from php
Phone calls and sms from phpDavid Stockton
667 views71 slides
Automating Your Workflow with Gulp.js - php[world] 2016 by
Automating Your Workflow with Gulp.js - php[world] 2016Automating Your Workflow with Gulp.js - php[world] 2016
Automating Your Workflow with Gulp.js - php[world] 2016Colin O'Dell
1.3K views68 slides
Drupal 8 - Core and API Changes by
Drupal 8 - Core and API ChangesDrupal 8 - Core and API Changes
Drupal 8 - Core and API ChangesShabir Ahmad
2.8K views39 slides
Introduction To Drupal by
Introduction To DrupalIntroduction To Drupal
Introduction To DrupalLauren Roth
4.5K views53 slides

More Related Content

Similar to Upgrading Drupal Modules - php[world] 2015

Drupal training-by-ruchiwebsolutions by
Drupal training-by-ruchiwebsolutionsDrupal training-by-ruchiwebsolutions
Drupal training-by-ruchiwebsolutionsphp2ranjan
36 views10 slides
Basic Introduction to Drupal by
Basic Introduction to DrupalBasic Introduction to Drupal
Basic Introduction to DrupalMurtaza Alvi
520 views28 slides
Fapi by
FapiFapi
FapiSteven Rifkin
765 views40 slides
Tools to Upgrade to Drupal 8 by
Tools to Upgrade to Drupal 8Tools to Upgrade to Drupal 8
Tools to Upgrade to Drupal 8DrupalGeeks
226 views2 slides
13th Sep, Drupal 7 advanced training by TCS by
13th Sep, Drupal 7 advanced training by TCS 13th Sep, Drupal 7 advanced training by TCS
13th Sep, Drupal 7 advanced training by TCS DrupalMumbai
6.9K views43 slides
Drupal @ MediaCamp Athens by
Drupal @ MediaCamp Athens Drupal @ MediaCamp Athens
Drupal @ MediaCamp Athens Nektarios Sylligardakis
854 views66 slides

Similar to Upgrading Drupal Modules - php[world] 2015(20)

Drupal training-by-ruchiwebsolutions by php2ranjan
Drupal training-by-ruchiwebsolutionsDrupal training-by-ruchiwebsolutions
Drupal training-by-ruchiwebsolutions
php2ranjan36 views
Basic Introduction to Drupal by Murtaza Alvi
Basic Introduction to DrupalBasic Introduction to Drupal
Basic Introduction to Drupal
Murtaza Alvi520 views
Tools to Upgrade to Drupal 8 by DrupalGeeks
Tools to Upgrade to Drupal 8Tools to Upgrade to Drupal 8
Tools to Upgrade to Drupal 8
DrupalGeeks226 views
13th Sep, Drupal 7 advanced training by TCS by DrupalMumbai
13th Sep, Drupal 7 advanced training by TCS 13th Sep, Drupal 7 advanced training by TCS
13th Sep, Drupal 7 advanced training by TCS
DrupalMumbai6.9K views
Absolute Beginners Guide to Drupal by Rod Martin
Absolute Beginners Guide to DrupalAbsolute Beginners Guide to Drupal
Absolute Beginners Guide to Drupal
Rod Martin4.9K views
"Paragraphs are more powerful than you can expect" from Vasily Jaremchuk for ... by DrupalCamp Kyiv
"Paragraphs are more powerful than you can expect" from Vasily Jaremchuk for ..."Paragraphs are more powerful than you can expect" from Vasily Jaremchuk for ...
"Paragraphs are more powerful than you can expect" from Vasily Jaremchuk for ...
DrupalCamp Kyiv1.3K views
Everything You Need to Know About the Top Changes in Drupal 8 by Acquia
Everything You Need to Know About the Top Changes in Drupal 8Everything You Need to Know About the Top Changes in Drupal 8
Everything You Need to Know About the Top Changes in Drupal 8
Acquia20.7K views
Drupal8 for Symfony Developers (PHP Day Verona 2017) by Antonio Peric-Mazar
Drupal8 for Symfony Developers (PHP Day Verona 2017)Drupal8 for Symfony Developers (PHP Day Verona 2017)
Drupal8 for Symfony Developers (PHP Day Verona 2017)
Drupal theming - a practical approach (European Drupal Days 2015) by Eugenio Minardi
Drupal theming - a practical approach (European Drupal Days 2015)Drupal theming - a practical approach (European Drupal Days 2015)
Drupal theming - a practical approach (European Drupal Days 2015)
Eugenio Minardi605 views
Building University Websites with the Drupal Content Management System by Mark Jarrell
Building University Websites with the Drupal Content Management SystemBuilding University Websites with the Drupal Content Management System
Building University Websites with the Drupal Content Management System
Mark Jarrell4.1K views
Taking your site from Drupal 6 to Drupal 7 by Phase2
Taking your site from Drupal 6 to Drupal 7Taking your site from Drupal 6 to Drupal 7
Taking your site from Drupal 6 to Drupal 7
Phase23.8K views
October 2016 - USG Rock Eagle - Everything You Need to Know to Plan Your Drup... by Eric Sembrat
October 2016 - USG Rock Eagle - Everything You Need to Know to Plan Your Drup...October 2016 - USG Rock Eagle - Everything You Need to Know to Plan Your Drup...
October 2016 - USG Rock Eagle - Everything You Need to Know to Plan Your Drup...
Eric Sembrat311 views
The Synergy of Drupal Hooks/APIs (Custom Module Development with ChartJS) by Ranel Padon
The Synergy of Drupal Hooks/APIs (Custom Module Development with ChartJS)The Synergy of Drupal Hooks/APIs (Custom Module Development with ChartJS)
The Synergy of Drupal Hooks/APIs (Custom Module Development with ChartJS)
Ranel Padon1.3K views
Drupal8 corporate training in Hyderabad by php2ranjan
Drupal8 corporate training in HyderabadDrupal8 corporate training in Hyderabad
Drupal8 corporate training in Hyderabad
php2ranjan75 views

More from Chris Tankersley

Docker is Dead: Long Live Containers by
Docker is Dead: Long Live ContainersDocker is Dead: Long Live Containers
Docker is Dead: Long Live ContainersChris Tankersley
53 views52 slides
Bend time to your will with git by
Bend time to your will with gitBend time to your will with git
Bend time to your will with gitChris Tankersley
194 views73 slides
Using PHP Functions! (Not those functions, Google Cloud Functions) by
Using PHP Functions! (Not those functions, Google Cloud Functions)Using PHP Functions! (Not those functions, Google Cloud Functions)
Using PHP Functions! (Not those functions, Google Cloud Functions)Chris Tankersley
177 views72 slides
Dead Simple APIs with OpenAPI by
Dead Simple APIs with OpenAPIDead Simple APIs with OpenAPI
Dead Simple APIs with OpenAPIChris Tankersley
311 views63 slides
Killer Docker Workflows for Development by
Killer Docker Workflows for DevelopmentKiller Docker Workflows for Development
Killer Docker Workflows for DevelopmentChris Tankersley
159 views64 slides
You Got Async in my PHP! by
You Got Async in my PHP!You Got Async in my PHP!
You Got Async in my PHP!Chris Tankersley
164 views140 slides

More from Chris Tankersley(20)

Using PHP Functions! (Not those functions, Google Cloud Functions) by Chris Tankersley
Using PHP Functions! (Not those functions, Google Cloud Functions)Using PHP Functions! (Not those functions, Google Cloud Functions)
Using PHP Functions! (Not those functions, Google Cloud Functions)
Chris Tankersley177 views
Killer Docker Workflows for Development by Chris Tankersley
Killer Docker Workflows for DevelopmentKiller Docker Workflows for Development
Killer Docker Workflows for Development
Chris Tankersley159 views
Docker for Developers - PHP Detroit 2018 by Chris Tankersley
Docker for Developers - PHP Detroit 2018Docker for Developers - PHP Detroit 2018
Docker for Developers - PHP Detroit 2018
Chris Tankersley865 views
BASHing at the CLI - Midwest PHP 2018 by Chris Tankersley
BASHing at the CLI - Midwest PHP 2018BASHing at the CLI - Midwest PHP 2018
BASHing at the CLI - Midwest PHP 2018
Chris Tankersley363 views
Docker for PHP Developers - php[world] 2017 by Chris Tankersley
Docker for PHP Developers - php[world] 2017Docker for PHP Developers - php[world] 2017
Docker for PHP Developers - php[world] 2017
Chris Tankersley821 views
Docker for PHP Developers - Madison PHP 2017 by Chris Tankersley
Docker for PHP Developers - Madison PHP 2017Docker for PHP Developers - Madison PHP 2017
Docker for PHP Developers - Madison PHP 2017
Chris Tankersley1.7K views
Docker for Developers - php[tek] 2017 by Chris Tankersley
Docker for Developers - php[tek] 2017Docker for Developers - php[tek] 2017
Docker for Developers - php[tek] 2017
Chris Tankersley1.1K views
OOP Is More Then Cars and Dogs - Midwest PHP 2017 by Chris Tankersley
OOP Is More Then Cars and Dogs - Midwest PHP 2017OOP Is More Then Cars and Dogs - Midwest PHP 2017
OOP Is More Then Cars and Dogs - Midwest PHP 2017
Chris Tankersley608 views
From Docker to Production - SunshinePHP 2017 by Chris Tankersley
From Docker to Production - SunshinePHP 2017From Docker to Production - SunshinePHP 2017
From Docker to Production - SunshinePHP 2017
Chris Tankersley853 views
Docker for Developers - Sunshine PHP by Chris Tankersley
Docker for Developers - Sunshine PHPDocker for Developers - Sunshine PHP
Docker for Developers - Sunshine PHP
Chris Tankersley812 views
Coming to Terms with OOP In Drupal - php[world] 2016 by Chris Tankersley
Coming to Terms with OOP In Drupal - php[world] 2016Coming to Terms with OOP In Drupal - php[world] 2016
Coming to Terms with OOP In Drupal - php[world] 2016
Chris Tankersley387 views
How We Got Here: A Brief History of Open Source by Chris Tankersley
How We Got Here: A Brief History of Open SourceHow We Got Here: A Brief History of Open Source
How We Got Here: A Brief History of Open Source
Chris Tankersley382 views

Recently uploaded

Digital Personal Data Protection (DPDP) Practical Approach For CISOs by
Digital Personal Data Protection (DPDP) Practical Approach For CISOsDigital Personal Data Protection (DPDP) Practical Approach For CISOs
Digital Personal Data Protection (DPDP) Practical Approach For CISOsPriyanka Aash
158 views59 slides
Why and How CloudStack at weSystems - Stephan Bienek - weSystems by
Why and How CloudStack at weSystems - Stephan Bienek - weSystemsWhy and How CloudStack at weSystems - Stephan Bienek - weSystems
Why and How CloudStack at weSystems - Stephan Bienek - weSystemsShapeBlue
238 views13 slides
CloudStack Managed User Data and Demo - Harikrishna Patnala - ShapeBlue by
CloudStack Managed User Data and Demo - Harikrishna Patnala - ShapeBlueCloudStack Managed User Data and Demo - Harikrishna Patnala - ShapeBlue
CloudStack Managed User Data and Demo - Harikrishna Patnala - ShapeBlueShapeBlue
135 views13 slides
The Power of Heat Decarbonisation Plans in the Built Environment by
The Power of Heat Decarbonisation Plans in the Built EnvironmentThe Power of Heat Decarbonisation Plans in the Built Environment
The Power of Heat Decarbonisation Plans in the Built EnvironmentIES VE
79 views20 slides
Kyo - Functional Scala 2023.pdf by
Kyo - Functional Scala 2023.pdfKyo - Functional Scala 2023.pdf
Kyo - Functional Scala 2023.pdfFlavio W. Brasil
457 views92 slides
CloudStack Object Storage - An Introduction - Vladimir Petrov - ShapeBlue by
CloudStack Object Storage - An Introduction - Vladimir Petrov - ShapeBlueCloudStack Object Storage - An Introduction - Vladimir Petrov - ShapeBlue
CloudStack Object Storage - An Introduction - Vladimir Petrov - ShapeBlueShapeBlue
138 views15 slides

Recently uploaded(20)

Digital Personal Data Protection (DPDP) Practical Approach For CISOs by Priyanka Aash
Digital Personal Data Protection (DPDP) Practical Approach For CISOsDigital Personal Data Protection (DPDP) Practical Approach For CISOs
Digital Personal Data Protection (DPDP) Practical Approach For CISOs
Priyanka Aash158 views
Why and How CloudStack at weSystems - Stephan Bienek - weSystems by ShapeBlue
Why and How CloudStack at weSystems - Stephan Bienek - weSystemsWhy and How CloudStack at weSystems - Stephan Bienek - weSystems
Why and How CloudStack at weSystems - Stephan Bienek - weSystems
ShapeBlue238 views
CloudStack Managed User Data and Demo - Harikrishna Patnala - ShapeBlue by ShapeBlue
CloudStack Managed User Data and Demo - Harikrishna Patnala - ShapeBlueCloudStack Managed User Data and Demo - Harikrishna Patnala - ShapeBlue
CloudStack Managed User Data and Demo - Harikrishna Patnala - ShapeBlue
ShapeBlue135 views
The Power of Heat Decarbonisation Plans in the Built Environment by IES VE
The Power of Heat Decarbonisation Plans in the Built EnvironmentThe Power of Heat Decarbonisation Plans in the Built Environment
The Power of Heat Decarbonisation Plans in the Built Environment
IES VE79 views
CloudStack Object Storage - An Introduction - Vladimir Petrov - ShapeBlue by ShapeBlue
CloudStack Object Storage - An Introduction - Vladimir Petrov - ShapeBlueCloudStack Object Storage - An Introduction - Vladimir Petrov - ShapeBlue
CloudStack Object Storage - An Introduction - Vladimir Petrov - ShapeBlue
ShapeBlue138 views
Business Analyst Series 2023 - Week 4 Session 7 by DianaGray10
Business Analyst Series 2023 -  Week 4 Session 7Business Analyst Series 2023 -  Week 4 Session 7
Business Analyst Series 2023 - Week 4 Session 7
DianaGray10139 views
How to Re-use Old Hardware with CloudStack. Saving Money and the Environment ... by ShapeBlue
How to Re-use Old Hardware with CloudStack. Saving Money and the Environment ...How to Re-use Old Hardware with CloudStack. Saving Money and the Environment ...
How to Re-use Old Hardware with CloudStack. Saving Money and the Environment ...
ShapeBlue166 views
State of the Union - Rohit Yadav - Apache CloudStack by ShapeBlue
State of the Union - Rohit Yadav - Apache CloudStackState of the Union - Rohit Yadav - Apache CloudStack
State of the Union - Rohit Yadav - Apache CloudStack
ShapeBlue297 views
Future of AR - Facebook Presentation by Rob McCarty
Future of AR - Facebook PresentationFuture of AR - Facebook Presentation
Future of AR - Facebook Presentation
Rob McCarty64 views
Elevating Privacy and Security in CloudStack - Boris Stoyanov - ShapeBlue by ShapeBlue
Elevating Privacy and Security in CloudStack - Boris Stoyanov - ShapeBlueElevating Privacy and Security in CloudStack - Boris Stoyanov - ShapeBlue
Elevating Privacy and Security in CloudStack - Boris Stoyanov - ShapeBlue
ShapeBlue222 views
Webinar : Desperately Seeking Transformation - Part 2: Insights from leading... by The Digital Insurer
Webinar : Desperately Seeking Transformation - Part 2:  Insights from leading...Webinar : Desperately Seeking Transformation - Part 2:  Insights from leading...
Webinar : Desperately Seeking Transformation - Part 2: Insights from leading...
Updates on the LINSTOR Driver for CloudStack - Rene Peinthor - LINBIT by ShapeBlue
Updates on the LINSTOR Driver for CloudStack - Rene Peinthor - LINBITUpdates on the LINSTOR Driver for CloudStack - Rene Peinthor - LINBIT
Updates on the LINSTOR Driver for CloudStack - Rene Peinthor - LINBIT
ShapeBlue206 views
VNF Integration and Support in CloudStack - Wei Zhou - ShapeBlue by ShapeBlue
VNF Integration and Support in CloudStack - Wei Zhou - ShapeBlueVNF Integration and Support in CloudStack - Wei Zhou - ShapeBlue
VNF Integration and Support in CloudStack - Wei Zhou - ShapeBlue
ShapeBlue203 views
Extending KVM Host HA for Non-NFS Storage - Alex Ivanov - StorPool by ShapeBlue
Extending KVM Host HA for Non-NFS Storage -  Alex Ivanov - StorPoolExtending KVM Host HA for Non-NFS Storage -  Alex Ivanov - StorPool
Extending KVM Host HA for Non-NFS Storage - Alex Ivanov - StorPool
ShapeBlue123 views
Business Analyst Series 2023 - Week 4 Session 8 by DianaGray10
Business Analyst Series 2023 -  Week 4 Session 8Business Analyst Series 2023 -  Week 4 Session 8
Business Analyst Series 2023 - Week 4 Session 8
DianaGray10123 views
"Surviving highload with Node.js", Andrii Shumada by Fwdays
"Surviving highload with Node.js", Andrii Shumada "Surviving highload with Node.js", Andrii Shumada
"Surviving highload with Node.js", Andrii Shumada
Fwdays56 views
NTGapps NTG LowCode Platform by Mustafa Kuğu
NTGapps NTG LowCode Platform NTGapps NTG LowCode Platform
NTGapps NTG LowCode Platform
Mustafa Kuğu423 views

Upgrading Drupal Modules - php[world] 2015

  • 1. Upgrading Drupal Modules Chris  Tankersley   php[world]  2015   php[world]  2015   1  
  • 2. Who Am I •  PHP  Programmer  for  over  10  years   •  Drupal  Developer  for  5  Years   •  Symfony  2,  Silex,  and  ZF2   Programmer   •  Maintainer  of  Social  Media  Bar   •  hJps://github.com/dragonmantank   php[world]  2015   2  
  • 4. Now we have to update our modules php[world]  2015   4  
  • 5. In the Before Times php[world]  2015   5  
  • 6. Drupal 6 to Drupal 7 php[world]  2015   6   Drupal  6   Drupal  7   Fix  All  the  API  Changes  
  • 7. Drupal 7 to Drupal 8 php[world]  2015   7   Drupal  7  
  • 8. Drupal 7 to Drupal 8 php[world]  2015   8   Drupal  7   PreJy  Much  Just  API  Changes  
  • 10. Gotta Share Them all php[world]  2015   10  
  • 12. Open Source and Give Back php[world]  2015   12  
  • 13. Gotta Update Them All php[world]  2015   13  
  • 15. New Structure modules/ | ‘- SocialMediaBar + src/ + SocialMediaBar.info.yml + SocialMediaBar.routing.yml php[world]  2015   15  
  • 16. Big Changes •  PSR4  Autoloading  (yay!)   •  YAML  config  files  instead  of  INI  config  files   •  Real  objects  for  nearly  everything   •  Much  cleaner  separa`on  of  concerns  for  code   php[world]  2015   16  
  • 17. Good News Much  of  your  business  logic  will   probably  be  copy-­‐paste  work   php[world]  2015   17  
  • 18. Bad News Much  of  the  documenta`on  is   s`ll  out  of  date   php[world]  2015   18  
  • 20. New Structure modules/ | ‘- SocialMediaBar + src/ + SocialMediaBar.info.yml + SocialMediaBar.routing.yml php[world]  2015   20  
  • 21. Move your module? •  Core  modules  live  in  core/modules  now   •  modules/  isn’t  off  limits  anymore!   •  Can  s`ll  live  in  sites/*/modules/ •  Naming  conven`ons  s`ll  apply   php[world]  2015   21  
  • 22. Info Files and YAML php[world]  2015   22  
  • 23. New Structure modules/ | ‘- SocialMediaBar + src/ + SocialMediaBar.info.yml + SocialMediaBar.routing.yml php[world]  2015   23  
  • 24. YAML Format •  Human  and  machine  readable  markup  language   •  Indenta`on-­‐based  markup   •  Supports  most  basic  types  of  data   •  Drupal  uses  it  for  module,  theme,  and  configura`on  informa`on   php[world]  2015   24  
  • 25. Update Your .info File •  We  are  switching  from  .ini  format  to  .yml  format   •  Modules  must  add  type: module  to  actually  show  up  as  a  module   php[world]  2015   25  
  • 26. socialmediabar.info name = Social Media Bar description = Adds a social media bar to different content types core = 7.x package = "Social Media" php = 5.3 stylesheets[all][] = css/socialmediabar.css scripts[] = js/socialmediabar.js dependencies[] = libraries configure = admin/config/services/socialmediabar php[world]  2015   26  
  • 27. socialmediabar.info.yml name: Social Media Bar description: Adds a social media bar to different content types core: 8.x type: module package: "Social Media" php[world]  2015   27  
  • 28. New Routing System php[world]  2015   28  
  • 29. New Structure modules/ | ‘- SocialMediaBar + src/ + SocialMediaBar.info.yml + SocialMediaBar.routing.yml php[world]  2015   29  
  • 30. hook_menu() is dead, long live Routing! •  This  is  all  moved  to  YAML  files   •  hook_menu()  is  now  split  into  routing.yml  and  links.*.yml •  routing.yml  handles  defining  URIs  and  route  config   •  links.*.yml  handles  how  links  appear  in  the  system   php[world]  2015   30  
  • 31. socialmediabar_menu() php[world]  2015   31   function socialmediabar_menu() { return array( 'admin/config/services/socialmediabar' => array( 'title' => 'Social Media Bar Admin', 'description' => 'Set up configuration options for the Social Media Bar', 'page callback' => 'drupal_get_form', 'page arguments' => array('socialmediabar_admin_form'), 'access arguments' => array('administer socialmediabar settings'), ), 'socialmediabar/countproxy' => array( 'title' => 'Social Media Bar Admin', 'page callback' => 'socialmediabar_countproxy', 'type' => MENU_CALLBACK, 'access arguments' => array('access content'), ), 'socialmediabar/shareproxy' => array( 'page callback' => 'socialmediabar_shareproxy', 'type' => MENU_CALLBACK, 'access arguments' => array('access content'), ), ); }
  • 32. socialmediabar.routing.yml php[world]  2015   socialmediabar.admin: path: "/admin/config/services/SocialMediaBar" defaults: _form: "DrupalSocialMediaBarFormAdminForm" _title: "Social Media Bar Admin" requirements: _permission: "access content" socialmediabar.count_proxy: path: "/socialmediabar/countproxy" defaults: _controller: "DrupalSocialMediaBarController CountProxyController::index" requirements: _access: 'TRUE’ 32  
  • 33. socialmediabar.links.menu.yml php[world]  2015   33   socialmediabar.admin: title: 'Social Media Bar Admin' parent: system.admin_config_services description: 'Configure the Social Media Bar sharing bar' route_name: socialmediabar.admin
  • 35. What is PSR-4 •  Defines  a  folder  structure  so  that  classes  can  be  loaded  on  demand,   instead  of  all  at  once   •  Unlike  PSR-­‐0,  allows  shallow  and  split  folders  for  loading   •  Much  more  efficient  than  the  the  old  files[]  key  in  .info •  No  more  require  at  the  top  of  your  files   •  All  of  your  code  will  now  live  in  src/ •  All  of  your  code  will  now  properly  namespaced   php[world]  2015   35  
  • 36. Drupal 8 is turtles objects all the way down php[world]  2015   36   hJps://upload.wikimedia.org/wikipedia/commons/4/47/River_terrapin.jpg  
  • 37. What is Namespacing? •  In  PHP,  a  way  to  separate  named  classes  and  ‘bundle’  them  together   •  Allows  for  renaming  of  classes  to  avoid  class  name  duplica`on   namespace DrupalSocialMediaBarController; use DrupalCoreFormConfigFormBase; use DrupalCoreControllerControllerBase as Base; php[world]  2015   37  
  • 38. Your Module’s Namespace Drupal[modulename] Lives  in   modules/[modulename]/src sites/all/modules/[modulename]/src sites/default/modules/[modulename]/src php[world]  2015   38  
  • 39. Why is it under the Drupal namespace? php[world]  2015   39  
  • 41. socialmediabar.routing.yml php[world]  2015   socialmediabar.admin: path: "/admin/config/services/SocialMediaBar" defaults: _form: "DrupalSocialMediaBarFormAdminForm" _title: "Social Media Bar Admin" requirements: _permission: "access content" socialmediabar.count_proxy: path: "/socialmediabar/countproxy" defaults: _controller: "DrupalSocialMediaBarController CountProxyController::index" requirements: _access: 'TRUE’ 41  
  • 42. New Structure modules/ | ‘- SocialMediaBar + src/ + Form/ + AdminForm.php + SocialMediaBar.info.yml + SocialMediaBar.routing.yml php[world]  2015   42  
  • 43. Form API Changes •  The  overall  syntax  is  s`ll  the  same   •  Form  State  is  now  an  objects  instead  of  an  array   •  Routes  now  call  forms  using  the  _form  key  instead  of   drupal_get_form() •  Forms  are  now  Objects,  extending  DrupalCoreFormFormBase •  System  forms  now  extend DrupalCoreFormConfigFormBase php[world]  2015   43  
  • 44. System/Config Forms php[world]  2015   44   namespace DrupalSocialMediaBarForm; use DrupalCoreFormConfigFormBase; use DrupalCoreFormFormStateInterface; class AdminForm extends ConfigFormBase { public function getEditableConfigNames() public function getFormId() public function buildForm(array $form, FormStateInterface $form_state) public function submitForm(array &$form, FormStateInterface $form_state) }
  • 46. Configuration is now Object-Based •  Injected  into  Controllers  and  forms   •  Can  be  accessed  by  Drupal::config()  if  needed   •  Handled  by  namespaced  areas  of  configura`on   •  socialmediabar.config •  Get    individual  keys  from  the  config,  set  them  to  change  values,  and   save  them  to  commit  the  changes   php[world]  2015   46  
  • 47. php[world]  2015   47   public function buildForm(array $form, FormStateInterface $form_state) { $config = $this->config('socialmediabar.config'); $enabled_networks = $config->get('socialmediabar_enabled_networks') ?: []; // ... $form['available_networks']['socialmediabar_enabled_networks'] = array( '#type' => 'checkboxes', '#options' => array( 'facebook' => t('Facebook'), 'twitter' => t('Twitter'), 'linkedin' => t('LinkedIn'), 'googleplus' => t('Google+'), 'email' => t('ShareThis Email'), ), '#default_value' => array_values($enabled_networks), ); // ... }
  • 48. php[world]  2015   48   public function submitForm(array &$form, FormStateInterface $form_state) { parent::submitForm($form, $form_state); $enabled_networks = $form_state->getValue('socialmediabar_enabled_networks'); $cache_time = $form_state->getValue('socialmediabar_cache_time’); $api_key = $form_state->getValue('socialmediabar_sharethis_apikey'); $config = $this->configFactory->getEditable('socialmediabar.config'); $config ->set('socialmediabar_enabled_networks’, $enabled_networks) ->set('socialmediabar_cache_time',$cache_time) ->set('socialmediabar_sharethis_apikey’, $api_key) ->save() ; }
  • 50. socialmediabar.routing.yml php[world]  2015   socialmediabar.admin: path: "/admin/config/services/SocialMediaBar" defaults: _form: "DrupalSocialMediaBarFormAdminForm" _title: "Social Media Bar Admin" requirements: _permission: "access content" socialmediabar.count_proxy: path: "/socialmediabar/countproxy" defaults: _controller: "DrupalSocialMediaBarControllerCountProxyController::index" requirements: _access: 'TRUE’ 50  
  • 51. New Structure modules/ | ‘- SocialMediaBar + src/ + Controller/ + CountProxyController.php + SocialMediaBar.info.yml + SocialMediaBar.routing.yml php[world]  2015   51  
  • 52. No More Callback Functions, Use Objects •  MENU_CALLBACK  func`ons  are  now  object::method  signatures   •  Controllers  extend  DrupalCoreControllerControllerBase •  View  output  is  now  handled  by  Response  Objects   php[world]  2015   52  
  • 53. php[world]  2015   53   namespace DrupalSocialMediaBarController; use DrupalCoreControllerControllerBase; use DrupalSocialMediaBarNetworkNetworkFactory; use SymfonyComponentHttpFoundationJsonResponse; class CountProxyController extends ControllerBase { public function index() { $url = Drupal::request()->get('url'); $provider = Drupal::request()->get('provider'); $errors = []; // ... if (count($errors)) { return new JsonResponse(['errors' => $errors], 400); } try { $network = NetworkFactory::getNetwork($provider); $count = $network->getCount($url); return new JsonResponse([$provider => ['outbound' => $count]]); } catch (Exception $e) { return new JsonResponse(['errors' => [$e->getMessage()]], 500); } } }
  • 55. New Structure modules/ | ‘- SocialMediaBar + src/ + templates/ + socialmediabar.html.twig + SocialMediaBar.info.yml + SocialMediaBar.routing.yml php[world]  2015   55  
  • 56. hook_theme(), booooooooooooo! •  Has  to  live  in  [modulename].module   •  Automa`cally  looks  in  the  templates/ folder  for  Twig  files   php[world]  2015   56   function SocialMediaBar_theme() { return array( 'socialmediabar' => array( 'template' => 'socialmediabar', 'variables' => array('api_key' => NULL, 'networks' => NULL), ), ); }
  • 57. Things are attached via #attached •  No  longer  have  drupal_add_css() or  drupal_add_js() •  Can  easily  bundle  up  CSS  and  JS  into  ‘libraries’   •  [modulename].libraries.yml   •  #attached  can  be  used  with  a  render  array,  or  form  descrip`ons   •  hJps://www.drupal.org/developing/api/8/assets   php[world]  2015   57  
  • 58. JavaScript settings changes •  Now  called  drupalSettings  instead  of  Drupal.settings •  Passed  via  a  drupalSettings  key  on  #attached php[world]  2015   58  
  • 59. php[world]  2015   59   public function render() { $config = Drupal::config('socialmediabar.config'); $drupalSettings = [ 'apiKey' => $config->get('socialmediabar_sharethis_apikey'), ]; $networks = $config->get('socialmediabar_enabled_networks'); $response = [ '#theme' => 'socialmediabar', '#api_key' => $drupalSettings['apiKey'], '#networks' => $networks, '#attached' => [ 'library' => ['SocialMediaBar/socialmediabar'], 'drupalSettings' => ['socialmediabar' => $drupalSettings], ], ]; return render($response); }
  • 61. Twig Extensions •  Allows  func`on  calls  inside  of  a  Twig  file   php[world]  2015   61  
  • 62. socialmediabar.services.yml php[world]  2015   62   services: socialmediabar.twig.renderer: class: DrupalSocialMediaBarTwigRendererExtension tags: - { name: twig.extension }
  • 63. php[world]  2015   63   namespace DrupalSocialMediaBarTwig; class RendererExtension extends Twig_Extension { public function getName() { return 'socialmediabar.renderer'; } public function getFunctions() { return [ new Twig_SimpleFunction('socialmediabar_render', [$this, 'render'], [ 'is_safe' => [true], ]) ]; } public function render() { // ... } }
  • 64. Thank You! hJp://ctankersley.com   chris@ctankersley.com   @dragonmantank     hJps://joind.in/14800   php[world]  2015   64