Download - Porting adsense module to Drupal 8
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.
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
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.
Variables
adsense.module define('ADSENSE_BASIC_ID_DEFAULT', ‘’)
adsense.admin.inc variable_get('adsense_basic_id', ADSENSE_BASIC_ID_DEFAULT)
adsense.install variable_del(‘adsense_basic_id’)
config/install/adsense.settings.yml adsense_basic_id: ‘'
src/Form/AdsenseIdSettings.php $config->get(‘adsense_basic_id’)
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'
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); }
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()}
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
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
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.
[email protected] www.wunderkraut.com Agnes-Pockels-Bogen 1, D1.019; 80992 München
Wunderkraut GmbH
d.o: jcnventura Phone: +49.89.85636307 [email protected]
João Ventura
Any questions??