Slide 1

Slide 1 text

Porting adsense to Drupal 8

Slide 2

Slide 2 text

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.

Slide 3

Slide 3 text

Lessons from the Drupal 7 port

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

Time to port to Drupal 8

Slide 6

Slide 6 text

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.

Slide 7

Slide 7 text

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’)

Slide 8

Slide 8 text

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'

Slide 9

Slide 9 text

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); }

Slide 10

Slide 10 text

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() }

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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.

Slide 14

Slide 14 text

[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??

Slide 15

Slide 15 text

Our difference is delivery