Drupal 8 Module Development
mrf.github.io/drupal8modules
github.com/mrf/drupal8examples
About Me:
Mark Ferree
@mrf
Encapsulation
http://pixabay.com/p-58177
Polymorphism & Inheritance
http://en.wikipedia.org/wiki/Butterfly#mediaviewer/File:Heliconius_mimicry.png
github.com/chapter-three/oophp-training
http://en.wikipedia.org/wiki/Diving#mediaviewer/File:Cliffjumping.jpg
name: Drupal 8 Module Development
description: Example code for http://mrf.github.io/drupal8-modules
package: Custom
type: module
version: 1.0
core: 8.x
dependencies:
- block
presentation.module
<\?php
Welcome to your first brain shifting opportunity
The old way
variable_set('presentation_seconds_to_wait', 300);
variable_get('presentation_seconds_to_wait', 24 * 3600);
The NEW way
\Drupal::config('presentation.settings')
->set('seconds_to_wait', 24 * 3600)
->save();
\Drupal::config('presentation.settings')->get('seconds_to_wait');
But what about my defaults?
Create a directory called config
Create a directory called install inside config
Get used to creating directories, trust me...
Create:
module_name.config_object_name.optional_sub_key.yml
presentation/config/install/presentation.settings.yml
seconds_to_wait: 20
Things to keep in mind:
- Do I really need a separate config object?
- Should assign config if working on it multiple times in a function
What if:
- Other modules need to modify it.
- My information is more complicated.
- I want to serialize a bunch of things.
- I'm thinking about a custom table in the DB to store my config.
See: Configuration Entities
The Old Way:
$items['presentation/callback'] = array(
'title' => 'My Special Callback',
'description' => "Arbitrary page for arbitrary reasons",
'page callback' => 'presentation_page_view',
'access arguments' => array('access content'),
'type' => MENU_LOCAL_TASK,
);
The New Way:
presentation.routing.yml
presentation.callback:
path: '/presentation/callback'
defaults:
_content: '\Drupal\presentation\Controller\PresentationController::myPage'
_title: 'My Special Callback'
requirements:
_permission: 'access content'
src/Controller/PresentationController.php
/**
* @file
* Contains \Drupal\presentation\Controller\PresentationController.
*/
namespace Drupal\presentation\Controller;
use Drupal\Core\Controller\ControllerBase;
------------------------------------------------
class PresentationController extends ControllerBase {
public function myPage() {
$seconds_to_wait = $this->config('presentation.settings')->get('seconds_to_wait');
$element = array(
'#markup' => $seconds_to_wait,
);
return $element;
}
}
The Old Way:
function hook_permission() {
return array(
'administer presentation' => array(
'title' => t('Administer presentation'),
'description' => t('Perform administration tasks for presentation.'),
),
);
}
The New Way:
presentation.permissions.yml
administer presentation:
title: 'Administer presentation'
description: 'Perform administration tasks for presentation.'
restrict access: TRUE
The Old Way:
function presentation_block_info() {
$blocks['presentation_simple'] = array(
'info' => t('Presentation: simple block'),
);
return $blocks;
}
function presentation_block_view($delta = '') {
switch ($delta) {
case 'presentation_simple':
$block['subject'] = t('Title of second block (presentation_simple)');
$block['content'] = "My block content";
break;
}
return $block;
}
src/Plugin/Block/SimpleBlock.php
/**
* @file
* Contains \Drupal\presentation\Plugin\Block\SimpleBlock.
*/
namespace Drupal\presentation\Plugin\Block;
use Drupal\block\Annotation\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Annotation\Translation;
/**
* Provides a 'Presentation: simple block' block.
*
* @Block(
* id = "presentation_simple",
* subject = @Translation("Presentation: simple block"),
* admin_label = @Translation("Presentation: simple block")
* )
*/
class SimpleBlock extends BlockBase {
/**
* {@inheritdoc}
*/
public function build() {
return array(
'#type' => 'markup',
'#markup' => $this->t("My block content"),
);
}
}