On Github mrf / drupal8modules
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
<\?php
variable_set('presentation_seconds_to_wait', 300); variable_get('presentation_seconds_to_wait', 24 * 3600);
\Drupal::config('presentation.settings') ->set('seconds_to_wait', 24 * 3600) ->save(); \Drupal::config('presentation.settings')->get('seconds_to_wait');
module_name.config_object_name.optional_sub_key.yml
presentation/config/install/presentation.settings.yml
seconds_to_wait: 20
$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, );
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; } }
function hook_permission() { return array( 'administer presentation' => array( 'title' => t('Administer presentation'), 'description' => t('Perform administration tasks for presentation.'), ), ); }
presentation.permissions.yml administer presentation: title: 'Administer presentation' description: 'Perform administration tasks for presentation.' restrict access: TRUE
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"), ); } }