You are here

protected function BlockStyleBase::getStylesFromVariables 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::getStylesFromVariables()

Get styles for a block set in a preprocess $variables array.

Parameters

array $variables: Block variables coming from a preprocess hook.

Return value

array|false Return the styles array or FALSE

2 calls to BlockStyleBase::getStylesFromVariables()
BlockStyle::themeSuggestion in src/Plugin/BlockStyle.php
Add theme suggestions for the block.
BlockStyleBase::build in src/Plugin/BlockStyleBase.php
Builds and returns the renderable array for this block style plugin.

File

src/Plugin/BlockStyleBase.php, line 170

Class

BlockStyleBase
Base class for Block style plugins.

Namespace

Drupal\block_style_plugins\Plugin

Code

protected function getStylesFromVariables(array $variables) {
  $styles = FALSE;

  // Ensure that we have a block id. If not, then the Layout Builder is used.
  if (empty($variables['elements']['#id'])) {
    $styles = $this
      ->getConfiguration();

    // Style config might not be set if this is happening in a hook so we will
    // check if a block_styles variable is set and get the config.
    if (empty($styles) && isset($variables['elements']['#configuration']['block_styles'][$this->pluginId])) {
      $this
        ->setConfiguration($variables['elements']['#configuration']['block_styles'][$this->pluginId]);
      $styles = $this
        ->getConfiguration();
    }
  }
  else {

    // Load the block config entity.

    /** @var \Drupal\block\Entity\Block $block */
    $block = $this->entityTypeManager
      ->getStorage('block')
      ->load($variables['elements']['#id']);

    // In the case that this block is placed via Context, it's not a "real"
    // block so it will be null. Attempt to get the block via variables.
    if (empty($block) && !empty($variables['elements']['#block'])) {
      $block = $variables['elements']['#block'];
    }

    // Check that we loaded a block.
    if (!empty($block)) {
      $styles = $block
        ->getThirdPartySetting('block_style_plugins', $this->pluginId);
      if ($styles) {
        $this
          ->setConfiguration($styles);
      }
    }
  }
  return $styles;
}