You are here

public function BlockStyleBase::build in Block Style Plugins 8.2

Same name and namespace in other branches
  1. 8 src/Plugin/BlockStyleBase.php \Drupal\block_style_plugins\Plugin\BlockStyleBase::build()

Builds and returns the renderable array for this block style plugin.

Parameters

array $variables: List of all variables sent to the theme system.

Return value

array A renderable array representing the content of the block.

Overrides BlockStyleInterface::build

File

src/Plugin/BlockStyleBase.php, line 86

Class

BlockStyleBase
Base class for Block style plugins.

Namespace

Drupal\block_style_plugins\Plugin

Code

public function build(array $variables) {

  // Is the Layout Builder being used for block components?
  $layout_builder_block = empty($variables['elements']['#id']) && empty($variables['layout']) ? TRUE : FALSE;
  $styles = $this
    ->getStylesFromVariables($variables);
  if (!$styles) {
    return $variables;
  }

  // Layout Builder needs a '#'.
  $hash = $layout_builder_block ? '#' : '';

  // Add styles to the configuration array so that they can be accessed in a
  // preprocess $variables['configuration']['block_styles'] or in a twig
  // template as {{ configuration.block_styles.plugin_id.field_name }}.
  $variables[$hash . 'configuration']['block_styles'][$this->pluginId] = $styles;

  // Automaticlly add each style value as a class.
  if (empty($this->pluginDefinition['disable_auto_classes'])) {
    foreach ($styles as $class) {

      // Don't put a boolean from a checkbox as a class.
      if (is_int($class)) {
        continue;
      }

      // Create the attributes class array if it doesn't exist yet.
      if (!isset($variables[$hash . 'attributes']['class'])) {
        $variables[$hash . 'attributes']['class'] = [];
      }

      // Add the class into an array unless it is already an array of classes.
      if (!is_array($class)) {
        $class = [
          $class,
        ];
      }

      // Add single or multiple classes.
      $variables[$hash . 'attributes']['class'] = array_merge($variables[$hash . 'attributes']['class'], $class);
    }
  }
  return $variables;
}