You are here

class StandardDisplayBuilder in Panels 8.3

Same name and namespace in other branches
  1. 8.4 src/Plugin/DisplayBuilder/StandardDisplayBuilder.php \Drupal\panels\Plugin\DisplayBuilder\StandardDisplayBuilder

The standard display builder for viewing a PanelsDisplayVariant.

Plugin annotation


@DisplayBuilder(
  id = "standard",
  label = @Translation("Standard")
)

Hierarchy

Expanded class hierarchy of StandardDisplayBuilder

2 files declare their use of StandardDisplayBuilder
InPlaceEditorDisplayBuilder.php in panels_ipe/src/Plugin/DisplayBuilder/InPlaceEditorDisplayBuilder.php
StandardDisplayBuilderTest.php in tests/src/Unit/StandardDisplayBuilderTest.php
Contains \Drupal\Tests\panels\Unit\StandardDisplayBuilderTest.

File

src/Plugin/DisplayBuilder/StandardDisplayBuilder.php, line 33

Namespace

Drupal\panels\Plugin\DisplayBuilder
View source
class StandardDisplayBuilder extends DisplayBuilderBase implements PluginWizardInterface, ContainerFactoryPluginInterface {

  /**
   * The context handler.
   *
   * @var \Drupal\Core\Plugin\Context\ContextHandlerInterface
   */
  protected $contextHandler;

  /**
   * The current user.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $account;

  /**
   * Constructs a new PanelsDisplayVariant.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin ID for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin definition.
   * @param \Drupal\Core\Plugin\Context\ContextHandlerInterface $context_handler
   *   The context handler.
   * @param \Drupal\Core\Session\AccountInterface $account
   *   The current user.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, ContextHandlerInterface $context_handler, AccountInterface $account) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->contextHandler = $context_handler;
    $this->account = $account;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('context.handler'), $container
      ->get('current_user'));
  }

  /**
   * Build render arrays for each of the regions.
   *
   * @param array $regions
   *   The render array representing regions.
   * @param array $contexts
   *   The array of context objects.
   *
   * @return array
   *   An associative array, keyed by region ID, containing the render arrays
   *   representing the content of each region.
   */
  protected function buildRegions(array $regions, array $contexts) {
    $build = [];
    foreach ($regions as $region => $blocks) {
      if (!$blocks) {
        continue;
      }
      $region_name = Html::getClass("block-region-{$region}");
      $build[$region]['#prefix'] = '<div class="' . $region_name . '">';
      $build[$region]['#suffix'] = '</div>';

      /** @var \Drupal\Core\Block\BlockPluginInterface[] $blocks */
      $weight = 0;
      foreach ($blocks as $block_id => $block) {
        if ($block instanceof ContextAwarePluginInterface) {
          $this->contextHandler
            ->applyContextMapping($block, $contexts);
        }
        if ($block
          ->access($this->account)) {
          $block_render_array = [
            '#theme' => 'block',
            '#attributes' => [],
            '#contextual_links' => [],
            '#weight' => $weight++,
            '#configuration' => $block
              ->getConfiguration(),
            '#plugin_id' => $block
              ->getPluginId(),
            '#base_plugin_id' => $block
              ->getBaseId(),
            '#derivative_plugin_id' => $block
              ->getDerivativeId(),
          ];

          // Build the block and bubble its attributes up if possible. This
          // allows modules like Quickedit to function.
          // See \Drupal\block\BlockViewBuilder::preRender() for reference.
          $content = $block
            ->build();
          if ($content !== NULL && !Element::isEmpty($content)) {
            foreach ([
              '#attributes',
              '#contextual_links',
            ] as $property) {
              if (isset($content[$property])) {
                $block_render_array[$property] += $content[$property];
                unset($content[$property]);
              }
            }
          }

          // If the block is empty, instead of trying to render the block
          // correctly return just #cache, so that the render system knows the
          // reasons (cache contexts & tags) why this block is empty.
          if (Element::isEmpty($content)) {
            $block_render_array = [];
            $cacheable_metadata = CacheableMetadata::createFromObject($block_render_array);
            $cacheable_metadata
              ->applyTo($block_render_array);
            if (isset($content['#cache'])) {
              $block_render_array['#cache'] += $content['#cache'];
            }
          }
          $block_render_array['content'] = $content;
          $build[$region][$block_id] = $block_render_array;
        }
      }
    }
    return $build;
  }

  /**
   * {@inheritdoc}
   */
  public function build(PanelsDisplayVariant $panels_display) {
    $regions = $panels_display
      ->getRegionAssignments();
    $contexts = $panels_display
      ->getContexts();
    $layout = $panels_display
      ->getLayout();
    $regions = $this
      ->buildRegions($regions, $contexts);
    if ($layout) {
      $regions = $layout
        ->build($regions);
    }
    return $regions;
  }

  /**
   * {@inheritdoc}
   */
  public function getWizardOperations($cached_values) {
    $operations = [];
    $operations['content'] = [
      'title' => $this
        ->t('Content'),
      'form' => PanelsContentForm::class,
    ];
    return $operations;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StandardDisplayBuilder::$account protected property The current user.
StandardDisplayBuilder::$contextHandler protected property The context handler.
StandardDisplayBuilder::build public function Renders a Panels display. Overrides DisplayBuilderBase::build 1
StandardDisplayBuilder::buildRegions protected function Build render arrays for each of the regions.
StandardDisplayBuilder::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create 1
StandardDisplayBuilder::getWizardOperations public function Retrieve a list of FormInterface classes by their step key in the wizard. Overrides PluginWizardInterface::getWizardOperations
StandardDisplayBuilder::__construct public function Constructs a new PanelsDisplayVariant. Overrides PluginBase::__construct 1
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.