You are here

public function SidrTrigger::build in Sidr: Accessible Mobile Menus 8

Same name and namespace in other branches
  1. 8.3 src/Plugin/Block/SidrTrigger.php \Drupal\sidr\Plugin\Block\SidrTrigger::build()
  2. 8.2 src/Plugin/Block/SidrTrigger.php \Drupal\sidr\Plugin\Block\SidrTrigger::build()

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/SidrTrigger.php, line 74

Class

SidrTrigger
Provides a trigger button with Sidr integration.

Namespace

Drupal\sidr\Plugin\Block

Code

public function build() {
  $conf = $this->configuration;
  $settings = $this->configFactory
    ->get('sidr.settings');

  // Determine sidr theme.
  $sidr_theme = $settings
    ->get('sidr_theme') ?: static::DEFAULT_THEME;

  // Wrapper element.
  $build = [
    '#type' => 'container',
    '#theme_wrappers' => [
      'container' => [
        '#attributes' => [
          'class' => [
            'sidr-trigger',
            'js-sidr-trigger',
          ],
          'data-sidr-options' => json_encode($this
            ->getSidrJsOptions()),
        ],
      ],
    ],
    '#attached' => [
      'library' => [
        'sidr/behaviors',
        'sidr/sidr.' . $sidr_theme,
      ],
    ],
  ];

  // Display icon, if any.
  if ($conf['trigger_icon']) {
    $build['icon'] = [
      '#type' => 'markup',
      '#markup' => $conf['trigger_icon'],
      '#prefix' => '<span class="sidr-trigger__icon">',
      '#suffix' => '</span>',
    ];
    $build['#theme_wrappers']['container']['#attributes']['class'][] = 'has-icon';
  }

  // Display text, if any.
  if ($conf['trigger_text']) {
    $build['text'] = [
      '#type' => 'html_tag',
      '#tag' => 'span',
      '#attributes' => [
        'class' => [
          'sidr-trigger__text',
        ],
      ],
      '#value' => $conf['trigger_text'],
    ];
    $build['#theme_wrappers']['container']['#attributes']['class'][] = 'has-text';
  }
  return $build;
}