Emixa Mendix Meetup 11 April 2024 about Mendix Native development
#D8CX: Upgrade your modules to Drupal 8 (Part 1 and 2)
1. #D8CX
Kate Marshalkina & Konstantin Komelin
DrupalCamp Kyiv 2013 Start Here
Upgrade your modules
to Drupal 8
2. A Little More About Us
2
DrupalCamp Kyiv 2013
[Moscow, Russia]
• UI Cache Clear module author
• Contributor to several modules
• Web developer at Licel LLC
[Saint Petersburg, Russia]
• Author of Yandex.Metrics, Pinned Site
• Contributor to modules and themes
• Co-organizer of Drupal Community in St. Petersburg
• Individual web developer, trainer and consultant
Kate Marshalkina kalabro
Konstantin Komelin KKomelin
6. #D8CX: Drupal 8 Contrib Experience
• Release top 40 modules together with D8
• 27 modules with #D8CX flag (Jun 8, 2013)
• Not only top modules are important!
#D8CX: I pledge that UI Cache Clear will have a full Drupal 8 release on the day that
Drupal 8 is released.
#D8CX: We pledge that Yandex.Metrics will have a full Drupal 8 release on the day
that Drupal 8 is released.
#D8CX: I pledge that Yandex Services Authorization API will have a full Drupal 8
release on the day that Drupal 8 is released.
6
DrupalCamp Kyiv 2013
7. Why should I upgrade?
7
DrupalCamp Kyiv 2013
• Be one of the first
• Prepare to D8
• Learn Drupal 8 API
• Help to community and Drupal
• Just for fun ;)
8. When should I start?
8
DrupalCamp Kyiv 2013
“In Villabajo they are still drilling the core and don’t have agreements about
many things, and in Villarriba others have already upgraded modules”
D8 begins
Mar 10, 2011
Alpha1 RC
Dec, 2013
Beta
May 20, 2013
Drupal 8.0
Sep-Oct, 2013
July, 2013
14. Before you start
14
DrupalCamp Kyiv 2013
• Update PHP to 5.3.10
• Reconcile to o_OP
• Get used to use https://drupal.org/list-changes
• Be ready to reinstall Drupal
• Prepare your rocket ;)
18. Settings form
18
DrupalCamp Kyiv 2013
namespace Drupalyandex_metricsForm;
use DrupalsystemSystemConfigFormBase;
class YandexMetricsCounterSettingsForm extends SystemConfigFormBase {
public function getFormID() {
return 'yandex_metrics_counter_settings';
}
//...
lib/Drupal/yandex_metrics/Form/YandexMetricsCounterSettingsForm.php
— Goodbye, system_settings_form().
19. Settings form
19
DrupalCamp Kyiv 2013
public function buildForm(array $form, array &$form_state) {
$form['path'] = array(
'#type' => 'details',
'#title' => t('Page specific tracking settings'),
);
// ...
return parent::buildForm($form, $form_state);
}
public function validateForm(array &$form, array &$form_state) { }
public function submitForm(array &$form, array &$form_state) { }
lib/Drupal/yandex_metrics/Form/YandexMetricsCounterSettingsForm.php
Override methods that you need
22. Config & State
22
DrupalCamp Kyiv 2013
// Get config value.
$counter_code = Drupal::config('yandex_metrics.settings')
->get('counter_code');
// Set config value.
Drupal::config('yandex_metrics.settings')
->set('counter_code', $counter_code)->save();
In files
// Get value of state variable.
$value = Drupal::state()->get('some_module.key');
// Set value of state variable.
Drupal::state()->set('some_module.key', $value);
In database
28. Block Plugin (practice)
28
LOREM IPSUM DOLOREM COMPANY Insert You Tagline Here
28
namespace Drupalhello_worldPluginBlock;
Now plugin will be found
automatically!
30. Block Plugin (practice)
30
DrupalCamp Kyiv 2013
Override methods that you need
protected function blockBuild() {
$this->configuration['label'] = t('Random number');
return array(
'#markup' => rand(1, 9),
);
}
//..
public function settings() { }
public function blockForm($form, &$form_state) { }
public function blockSubmit($form, &$form_state) { }
..lib/Drupal/hello_world/Plugin/Block/HelloWorldRandomBlock.php
35. Visualization API (8.x)
35
DrupalCamp Kyiv 2013
https://drupal.org/sandbox/Niremizov/1985870
Thx Christophe Van Gysel!
Module features:
• Theme hook
• Plugin service (custom
chart handlers)
• Views Style Plugin
36. Views
36
DrupalCamp Kyiv 2013
• Inside core and already works
• Uses core Plugin System
• Export/Import using Config Entities
• Small API changes
CamelCase
hook_views_api, hook_views_plugin
Tip: How to create default view in Drupal 8:
Copy sitesdefaultfilesconfig_{HASH}activeconfigviews.view.{view_name}.yml
Paste modules{module_name}configviews.view.{view_name}.yml
39. DrupalCamp Kyiv 2013
KEEP CALM
AND
CLEAR CACHE
And Drupal 8 says:
— “Fatal error: Call to undefined function cache_clear_all()”
40. Cache System
Cache Tags instead of wildcards
DrupalCamp Kyiv 2013
Cached Item 1
Tag 1
Tag 2
Tag 3
Cached Item 2 Tag 2 Cached Item 3
Tag 1
Tag 3
cache()->invalidateTags(array('Tag 2'));
Cached Item 3
Tag 1
Tag 3
Cached Item 1
Tag 1
Tag 2
Tag 3
Cached Item 2 Tag 2
*
Note: Drupal calculates “checksum” for every cache tag when read from cache!
40
41. Cache System
41
DrupalCamp Kyiv 2013
// Set cache to 'page' bin.
cache('page')->set($cid, $data, $expire, $tags);
New Cache API
// Truncate cache table.
cache()->deleteAll();
// Marks cache items from all bins with 'content' tag as invalid.
cache_invalidate_tags(array('content' => TRUE));
// Get multiple cache items.
cache('field')->getMultiple($cids);
// Get item even it is invalid.
cache()->get('field:user:1', TRUE);
42. Automated tests
42
DrupalCamp Kyiv 2013
namespace Drupalyandex_metricsTests;
use DrupalsimpletestWebTestBase;
class CounterTest extends WebTestBase {
public static $modules = array('yandex_metrics');
public static function getInfo() { }
public function setUp() {
parent::setUp();
}
public function tearDown() { }
lib/Drupal/yandex_metrics/Tests/CounterTest.php
44. References
44
DrupalCamp Kyiv 2013
• Change records for Drupal core
http://drupal.org/list-changes
• [May 22, 2013] Alex Bronstein (effulgentsia) - Upgrading your modules to
Drupal 8
http://portland2013.drupal.org/session/upgrading-your-modules-drupal-8
• [Feb 8, 2013] Angie Byron (webchick) - Step-by-step: Converting modules
from Drupal 7 to Drupal 8
http://webchick.net/node/118
• [Nov 4, 2012] Tim Plunkett (tim.plunkett) - D8CX: The Reckoning
http://2012.badcamp.net/program/sessions/d8cx-reckoning
45. !
DrupalCamp Kyiv 2013
Thank you for your attention!
Konstantin Komelin
@KKomelin
konstantin@komelin.com
Kate Marshalkina
@kalabro
marshalkina@licel.ru