You are here

public function DisclaimerEmailBlock::build in Disclaimer 8

Builds and returns the renderable array for this block plugin.

If a block should not be rendered because it has no content, then this method must also ensure to return no content: it must then only return an empty array, or an empty array with #cache set (with cacheability metadata indicating the circumstances for it being empty).

Return value

array A renderable array representing the content of the block.

Overrides BlockPluginInterface::build

See also

\Drupal\block\BlockViewBuilder

File

src/Plugin/Block/DisclaimerEmailBlock.php, line 211

Class

DisclaimerEmailBlock
Provides a 'DisclaimerEmailBlock' block.

Namespace

Drupal\disclaimer\Plugin\Block

Code

public function build() {
  $disclaimer_email_id = 'disclaimer_email_' . Html::escape($this->configuration['machine_name']);

  // Identify block by class with machine name.
  $build = [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        $disclaimer_email_id,
        'disclaimer_email__noscript',
      ],
    ],
  ];

  // Include JS to handle popup and hiding.
  $build['#attached']['library'][] = 'disclaimer/disclaimer_email';

  // Pass settings to JS.
  $build['#attached']['drupalSettings']['disclaimer_email'][$disclaimer_email_id] = [
    'redirect' => $this->configuration['redirect'],
  ];

  // Render disclaimer.
  $build['disclaimer_email_block_disclaimer'] = [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'disclaimer_email__disclaimer',
      ],
    ],
    '#markup' => check_markup($this->configuration['disclaimer']['value'], $this->configuration['disclaimer']['format']),
  ];

  // Render popup HTML.
  $build['disclaimer_email_block_challenge'] = [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'disclaimer_email__challenge',
        'hidden',
      ],
      'title' => [
        Html::escape($this
          ->label()),
      ],
    ],
    '#markup' => check_markup($this->configuration['challenge']['value'], $this->configuration['challenge']['format']),
  ];

  // Render E-mail challenge.
  $build['disclaimer_email_block_challenge']['disclaimer_email_block_email_match'] = $this->formBuilder
    ->getForm('\\Drupal\\disclaimer\\Form\\DisclaimerEmailMatchForm');
  $build['disclaimer_email_block_challenge']['disclaimer_email_block_email_match']['block_id']['#value'] = $this->configuration['machine_name'];
  return $build;
}