2. Hooks are functions
that are called for
each module when
system events
happen, e.g. page
load or user login
Hooks
3. Drupal7VsDRUPal8
Number of hooks in Drupal 7 : 352
Number of hooks in Drupal 8 : 338
Deprecated hooks : 83
New hooks in Drupal 8 : 56
Renamed hooks : at least 4
4. SOMEIMPORTANTDEPRECATEDHOOKSâhook_menu
hook_menu() -> Drupal 8 APIs should start with
defining the routes for the items.
<?php
# Drupal 7 menu item
$items['admin/content/book'] = array(
'title' => 'Books',
'description' => "Manage your site's book outlines.",
'page callback' => 'book_admin_overview',
'access arguments' => array('administer book outlines'),
'type' => MENU_LOCAL_TASK,
'file' => 'book.admin.inc',
);
?>
6. SOMEIMPORTANTDEPRECATEDHOOKSâhook_menu
hook_init() -> Drupal 8 has adopted symfony as a
part of its core. It uses Symfony kernel and events
to do the same now.
<?php
function mymodule_init() {
if (!empty($_GET['redirect-me'])) {
drupal_goto('http://example.com/');
}
}
?>
7. SOMEIMPORTANTDEPRECATEDHOOKSâhook_menu
mymodule.services.yml
services:
mymodule_event_subscriber:
class: DrupalmymoduleEventSubscriberMymoduleSubscriber
tags:
- {name: event_subscriber}
Src/ EventSubscriber/ MymoduleSubscriber.php
<?php
namespace DrupalmymoduleEventSubscriber;
use SymfonyComponentHttpFoundationRedirectResponse;
use SymfonyComponentHttpKernelKernelEvents;
use SymfonyComponentHttpKernelEventGetResponseEvent;
use SymfonyComponentEventDispatcherEventSubscriberInterface;
class MymoduleSubscriber implements EventSubscriberInterface {
public function checkForRedirection(GetResponseEvent $event) {
if ($event->getRequest()->query->get('redirect-me')) {
$event->setResponse(new RedirectResponse('http://example.com/'));
}
}
/**
* {@inheritdoc}
*/
static function getSubscribedEvents() {
$events[KernelEvents::REQUEST][] = array('checkForRedirection');
return $events;
}
}
?>
8. SOMEIMPORTANTDEPRECATEDHOOKSâhook_menu
If your module just needs to add css and/or
javascript, hook_page_attachments should be
used in.
function MYMODULE_page_attachments(array &$attachments) {
$attachments ['#attached']['library'][] =
'modulename/libraryname';
}
14. EXAMPLEâVIEWSAPI<?php
use DrupalviewsPluginviewsqueryQueryPluginBase;
use DrupalviewsViewExecutable;
function my_examples_views_query_substitutions(ViewExecutable $view){
$account = Drupal::currentUser();
return array(
'***CURRENT_TIME***' => REQUEST_TIME,
);
//echo "<pre>";print_r($view->getQuery());echo "</pre>";exit();
}
function my_examples_views_query_alter(ViewExecutable $view,QueryPluginBase $query) {
if ($view->id() == 'first_view') {
// Traverse through the 'where' part of the query.
foreach ($query->where as &$condition_group) {
foreach ($condition_group['conditions'] as &$condition) {
// If this is the part of the query filtering on title, chang the
// condition to filter on node ID.
if ($condition['field'] == 'node_field_data.title') {
$condition = array(
'field' => 'node_field_data.title',
'value' => 'Test 2',
'operator' => '=',
);
}
}
}
}
}
15. We can build Entity
types, which can
make Bundles, to
which we can add
Fields and then
create Entities.
Entities
16. EntityAPI
Whatâs an entity?
An entity type is a base class
A bundle is an extended class
A field is a class member,
property, variable or field
instance (depending on your naming
preference)
An entity is an object or instance
of a base or extended class
Drupal 7 Entity Types?
Nodes (content)
Comments
Files
Taxonomy terms
Taxonomy vocabularies
Users
20. Drupal8hasconfigurationManagement!!!
Configuration
Stored in YAML files, not database
Canonical workflow: Change on dev,
Deploy to prod
Content
Stored in database
Canonical workflow: Changes on
prod constantly.
Drupal is not just a Content Management System: Itâs Content & Configuration
Management System ď
22. Twofamiliesofentitytypes
Content
1) Revisionable
2) Uses content translation api
3) Fieldable
4) All data is the field and
follows the field data model
5) Integrated with REST
6) ID is autogenerated, stable,
non-recyclable
Config
1) Non Revisionable
2) Uses config translation api
3) Not Fieldable
4) Data is PHP scalars/arrays and
follows a type-defined schema.
5) Not integrated with REST
6) ID is a machine name that can
be edited & recycled.
26. EXAMPLEâENTITYAPI<?php
use DrupalCoreEntityDisplayEntityViewDisplayInterface;
use DrupalCoreEntityEntityInterface;
use DrupalCoreFormFormStateInterface;
use DrupalnodeEntityNodeType;
use DrupalnodeNodeTypeInterface;
/**
* Implements hook_form_BASE_FORM_ID_alter().
*/
function reusable_forms_form_node_type_form_alter(&$form, FormStateInterface $form_state) {
$form['reusable_forms'] = array(
'#type' => 'details',
'#title' => t('Reusable forms'),
'#group' => 'additional_settings',
);
$node_type = $form_state->getFormObject()->getEntity();
$form['reusable_forms']['reusable_forms_enabled'] = array(
'#type' => 'checkbox',
'#title' => t('Enable reusable forms'),
'#description' => t('Check this box if you would like a reusable form on this node type.'),
'#default_value' => $node_type->getThirdPartySetting('reusable_forms', 'enabled', 0),
);
27. EXAMPLEâENTITYAPI$form_plugins = Drupal::service('plugin.manager.reusable_forms')->getDefinitions();
$options = [];
foreach ($form_plugins as $name => $plugin) {
$options[$name] = $plugin['name'];
}
$form['reusable_forms']['reusable_forms_plugin'] = array(
'#type' => 'radios',
'#title' => t('Available forms'),
'#default_value' => $node_type->getThirdPartySetting('reusable_forms', 'plugin', 'basic_form'),
'#options' => $options,
'#description' => t('The available forms you can choose from for this node type.'),
'#states' => array(
'visible' => array(
':input[name="reusable_forms_enabled"]' => array('checked' => TRUE),
),
),
);
$form['#entity_builders'][] = 'reusable_forms_form_node_type_form_builder';
}
28. EXAMPLEâENTITYAPI/**
* Entity form builder for the node type form to map some values to third party
* settings
*/
function reusable_forms_form_node_type_form_builder($entity_type, NodeTypeInterface $type, &$form, FormStateInterface $form_state) {
if ($form_state->getValue('reusable_forms_enabled') === 1) {
$type->setThirdPartySetting('reusable_forms', 'enabled', 1);
$type->setThirdPartySetting('reusable_forms', 'plugin', $form_state->getValue('reusable_forms_plugin'));
return;
}
$type->unsetThirdPartySetting('reusable_forms', 'enabled');
$type->unsetThirdPartySetting('reusable_forms', 'plugin');
}