Skip to main content

Blog Post

Preparing for Drupal 8 Development - Blocks, Forms, and Pages

The transition from Drupal 7 to 8 can be intimidating, particularly if you're new to Symfony 2 or OOP.

Whereas hooks are used in Drupal 7 and before, Drupal 8 makes use of Object Oriented classes. This is a bit different because you are taking the basic definition of something like a block and then are "extending" it to turn an abstract idea into something concrete.

In any case, it is often easier to look at examples, so rather than talking in abstract terms, here are some code snippets related to making blocks, pages, and forms in Drupal 8:

Defining a Block

In Drupal 8, blocks are defined by extending class “BlockBase”:

namespace Drupal\example_block\Plugin\Block;
use Drupal\block\BlockBase;

class blockExampleBlock extends BlockBase {

  /**
   * Implements \Drupal\block\BlockBase::build().
   */
  public function build() {
    $build = array();

    // Define block content:
    $content = ‘Example block content.’;
    $build['#markup'] = $content;

    // Add any JS or perhaps CSS
    $module_path = drupal_get_path('module', 'example_block'); 

    $build['#attached']['css'][] = $module_path .
      '/css/jquery_example_block.css';

    $build['#attached']['js'][] = $module_path .
      '/js/jquery_example_block.js';

    return $build;
  }
}

 

Defining a Form

If you’re familiar with the Drupal 7 Form API, picking up forms in D8 will not be a problem. The main difference is the cruft surrounding the familiar $form array.

namespace Drupal\example\Form;
use Drupal\Core\Form\FormBase;

class ExampleForm extends FormBase {

  /**
   * Implements \Drupal\Core\Form\FormBase::buildForm().
   */
  public function buildForm(array $form, array &$form_state) {
    // Define your form here, as usual.
    $form[’example_form_email'] = array(
      '#type' => ’email',
      '#title' => t(‘Please enter your email address.'),
    ); 
    return $form;
  }

  /**
   * Implements \Drupal\Core\Form\FormBase::submitForm().
   */
  public function submitForm(array &$form, array &$form_state) {
    $example_email = $form_state['values']['example_form_email’]);
    config(‘example_form.email')
      ->set('example_email', $example_email)
      ->save();
  }
}

    Although the form API is very familiar, there are several new types of form elements available for use. These not only improve semantics, but also allow facilitate input; for example, “tel” might bring up a telephone number pad on a mobile device:

  • Color
  • Date
  • Email
  • Number
  • Range (Numeric)
  • Tel
  • URL

Defining a Custom Page

First, define routing info in page_example.routing.yml:

page_example.content:
  path: '/example-page'
  defaults:
    _title: 'Page Example Title'
    _content: '\Drupal\page_example\Controller\PageExampleController::content'
  requirements:
    _permission: 'access content'

    Here, PageController is extended and the output is defined:

namespace Drupal\page_example\Pages
use Drupal\page_controller\Controller\PageController;

class PageExampleController extends PageController {

  /**
   * Implements \Drupal\page_controller\Controller\PageController::content().
   */
  public function content() {
    // Define output:
    $output = t(‘Page example content.’);

    return array(
      '#markup' => $output,
    );
  }
} 

 

Let’s continue the discussion below. What are your thoughts on transitioning from D7 to D8?

Do you have any concerns or foresee any difficulties?

 

 

 

Drupal 8 Resource Guide

Access icon Up arrow icon Drupal 8 icon Facebook icon - white Facebook icon - blue outline Facebook icon - yellow Hollow right arrow icon Hollow right arrow icon - white LinkedIn icon - white LinkedIn icon - hollow LinkedIn icon - blue outline LinkedIn icon - yellow Mediacurrent wordmark Quote icon Twitter icon - white Twitter icon - hollow Twitter icon - blue outline Twitter icon - yellow Youtube icon - white Youtube icon - yellow