Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Porting adsense module to Drupal 8

Porting adsense module to Drupal 8

Port of the adsense module from Drupal 7 to Drupal 8. Lessons learned, and a few of the major differences between D7 and D8 module development. Advise to use the drupalmoduleupgrader module to handle the automatic parts.

João Ventura

August 14, 2015
Tweet

More Decks by João Ventura

Other Decks in Programming

Transcript

  1. Module Background • adsense module created in 2005 by Khalid

    Baheyeldin (4.7 and 5) • I ported the module to Drupal 6 in 2008 (with a backport to Drupal 5), and have been maintaining it ever since. • Support for pre-2007 AdSense ad codes and for the new Managed Ads (synchronous and asynchronous). Also supports Custom search engines. • Ad-blocker detection. • Used in over 12.000 sites.
  2. Converting modules from 6 to 7 ‣ The bible of

    6>7 ports, documenting 264 changes: ‣ https://www.drupal.org/update/modules/6/7 ‣ Coder upgrade module ‣ Don’t chase core, wait for beta releases ‣ In retrospective, seems trivial ‣ New Database layer (Object-oriented!) ‣ hook_nodeapi -> hook_node_xxx ‣ hook_block -> hook_block_xxx
  3. Converting modules from 7 to 8 ‣ The future bible

    of 7>8 ports (currently very incomplete) ‣ https://www.drupal.org/update/modules/7/8 ‣ Drupal Module Upgrader (DMU) module ‣ https://www.drupal.org/project/drupalmoduleupgrader ‣ Beta releases are out, and Drupal 8.0.0 will be released “soon”. ‣ It’s only called Drupal because it’s the work of the same community ‣ Drupal is mostly object-oriented now (exc. some hook_*) ‣ Symfony, HTML5, Twig, YAML, WYSIWYG editor, etc.
  4. Menu routing adsense.module function adsense_menu() { $items = array(); $items['admin/settings/adsense']

    = array( 'title' => 'AdSense', 'description' => ‘…’, 'page callback' => 'drupal_get_form', 'page arguments' => array('adsense_main_settings'), 'access arguments' => array('administer adsense'), 'file' => 'adsense.admin.inc', ); adsense.routing.yml adsense.main_settings: path: /admin/config/services/adsense defaults: _title: AdSense _form: \Drupal\adsense\Form \AdsenseMainSettings requirements: _permission: 'administer adsense'
  5. Configuration form adsense.admin.inc function adsense_id_settings() { $form['adsense_basic_id'] = array( '#type'

    => 'textfield', '#title' => t(…), '#required' => TRUE, '#default_value' => variable_get(), '#description' => t(…), ); $form['#validate'][] = '_adsense_id_settings_validate'; return system_settings_form($form); } src/Form/AdsenseIdSettings.php class AdsenseIdSettings extends ConfigFormBase { public function getFormId() { return 'adsense_id_settings'; } protected function getEditableConfigNames() { return ['adsense.settings']; } public function buildForm(array $form, FormStateInterface $form_state) { $form['adsense_basic_id'] = [ '#type' => 'textfield', '#title' => t(…), '#required' => TRUE, '#default_value' => $config->get(…), '#pattern' => 'pub-[0-9]+', '#description' => t(…), ]; return parent::buildForm($form, $form_state); }
  6. Blocks adsense_managed.module function adsense_managed_block_info() function adsense_managed_block_configure($delta = ‘’) function adsense_managed_block_save($delta

    = ‘', $edit = array()) function adsense_managed_block_view($delta = ‘') managed/src/Plugin/Block/ManagedAdBlock.php /** * Provides an AdSense managed ad block. * * @Block( * id = "adsense_managed_ad_block", * admin_label = @Translation("Managed ad"), * category = @Translation("Adsense") * ) */ class ManagedAdBlock extends BlockBase implements AdBlockInterface { public function defaultConfiguration() public function build() public function buildConfigurationForm(array $form, FormStateInterface $form_state) public function blockSubmit($form, FormStateInterface $form_state) public function getCacheMaxAge() public function isCacheable() }
  7. Current status ‣ Available at http://drupal.org/project/adsense ‣ http://ftp.drupal.org/files/projects/adsense-8.x-1.x-dev.tar.gz ‣ Drupal-specific

    code obsoleted in Drupal 8 ‣ DMU converted most of this, leaving dead code and @FIXMEs ‣ Converted all the sub-modules to OO plugins ‣ Only 3 .module files remain ‣ adsense.module: 2 theme and 1 API function ‣ adsense_managed.module: 1 hook_preprocess_block function ‣ revenue_sharing_basic.module: port to D8 TBD
  8. Drupal Module Upgrader • Handles the boring parts • Converts

    module.info to module.info.yml • Converts your hook_menu() to module.routing.yml • Converts the configuration forms to src/Form/FormName.php • etc. • NOT a magic wand. You still have to port your functionality to work with D8
  9. Lessons Learned • Time to port is NOW • There

    won’t be a wait period between core and Views releases. • It’s a complete rewrite of your code. Approach it as a new project. • If you can, make it a plug-in. • Make your code shine with objects. • Don’t stop until that .module file is empty.