You are here

class PanelsEverywhereDisplayVariant in Panels Everywhere 8.4

Same name and namespace in other branches
  1. 8 src/Plugin/DisplayVariant/PanelsEverywhereDisplayVariant.php \Drupal\panels_everywhere\Plugin\DisplayVariant\PanelsEverywhereDisplayVariant

Provides a display variant that simply contains blocks.

@todo: This shouldn't be necessary - the PanelsDisplayVariant should implement PageVariantInterface, because, it's easy and then it can be used to render the full page.

Plugin annotation


@DisplayVariant(
  id = "panels_everywhere_variant",
  admin_label = @Translation("Panels Everywhere")
)

Hierarchy

Expanded class hierarchy of PanelsEverywhereDisplayVariant

1 file declares its use of PanelsEverywhereDisplayVariant
PanelsEverywhereRouteSubscriberTest.php in tests/src/Unit/Routing/PanelsEverywhereRouteSubscriberTest.php

File

src/Plugin/DisplayVariant/PanelsEverywhereDisplayVariant.php, line 24

Namespace

Drupal\panels_everywhere\Plugin\DisplayVariant
View source
class PanelsEverywhereDisplayVariant extends PanelsDisplayVariant implements PageVariantInterface {

  /**
   * The renderer.
   *
   * @var \Drupal\Core\Render\RendererInterface
   */
  protected $renderer;

  /**
   * The render array representing the main page content.
   *
   * @var array
   */
  protected $mainContent = [];

  /**
   * The title for the display variant.
   *
   * @var string
   */
  protected $title;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    $instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
    $instance->renderer = $container
      ->get('renderer');
    return $instance;
  }

  /**
   * Sets the title for the page being rendered.
   *
   * @param string|array $title
   *   The page title: either a string for plain titles or a render array for
   *   formatted titles.
   *
   * @return $this
   */
  public function setTitle($title) {
    $this->title = (string) (is_array($title) ? $this->renderer
      ->renderRoot($title) : $title);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function setMainContent(array $main_content) {
    $this->mainContent = $main_content;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function build() {
    $main_content_included = NULL;
    $this
      ->setPageTitle($this->title);
    foreach ($this
      ->getRegionAssignments() as $region => $blocks) {
      if (!$blocks) {
        continue;
      }
      foreach ($blocks as $block_id => $block) {
        if ($block instanceof MainContentBlockPluginInterface) {
          $block
            ->setMainContent($this->mainContent);
          $main_content_included = [
            $region,
            $block_id,
          ];
        }
      }
    }

    // Build it the render array!
    $build = parent::build();

    // Copied from BlockPageVariant.php.
    // The main content block cannot be cached: it is a placeholder for the
    // render array returned by the controller. It should be rendered as-is,
    // with other placed blocks "decorating" it.
    if (!empty($main_content_included)) {
      list($region, $block_id) = $main_content_included;
      unset($build[$region][$block_id]['#cache']['keys']);
    }
    return $build;
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildConfigurationForm($form, $form_state);
    $form['route_override_enabled'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Enable page-manager route override'),
      '#default_value' => $this
        ->isRouteOverrideEnabled(),
      '#description' => $this
        ->t('The default behaviour of page-manager is create a route for the specified path and override any existing route (which effectively replaces that route). This behaviour is not desired for panels_everywhere as it prevents the original content of the route from being rendered properly. This is why panels_everywhere will remove those overrides by default. You may want do enable route overrides in cases you do not want the original route to be processed.'),
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    parent::submitConfigurationForm($form, $form_state);
    $this->configuration['route_override_enabled'] = $form_state
      ->getValue('route_override_enabled');
  }

  /**
   * Denotes whether route overrides are enabled for this variant.
   *
   * @return bool
   *   True if route overrides are enabled for this variant, false otherwise.
   */
  public function isRouteOverrideEnabled() {
    return !empty($this->configuration['route_override_enabled']) ? $this->configuration['route_override_enabled'] : FALSE;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CacheableDependencyInterface::getCacheContexts public function The cache contexts associated with this object. 34
CacheableDependencyInterface::getCacheMaxAge public function The maximum age for which this object may be cached. 34
CacheableDependencyInterface::getCacheTags public function The cache tags associated with this object. 27
ConfigurableInterface::defaultConfiguration public function Gets default configuration for this plugin. 11
ConfigurableInterface::getConfiguration public function Gets this plugin's configuration. 12
ConfigurableInterface::setConfiguration public function Sets the configuration for this plugin instance. 12
DependentPluginInterface::calculateDependencies public function Calculates dependencies for the configured plugin. 19
PanelsEverywhereDisplayVariant::$mainContent protected property The render array representing the main page content.
PanelsEverywhereDisplayVariant::$renderer protected property The renderer.
PanelsEverywhereDisplayVariant::$title protected property The title for the display variant.
PanelsEverywhereDisplayVariant::build public function Builds and returns the renderable array for the display variant. Overrides VariantInterface::build
PanelsEverywhereDisplayVariant::buildConfigurationForm public function Form constructor. Overrides PluginFormInterface::buildConfigurationForm
PanelsEverywhereDisplayVariant::create public static function
PanelsEverywhereDisplayVariant::isRouteOverrideEnabled public function Denotes whether route overrides are enabled for this variant.
PanelsEverywhereDisplayVariant::setMainContent public function Sets the main content for the page being rendered. Overrides PageVariantInterface::setMainContent
PanelsEverywhereDisplayVariant::setTitle public function Sets the title for the page being rendered. Overrides PageVariantInterface::setTitle
PanelsEverywhereDisplayVariant::submitConfigurationForm public function Form submission handler. Overrides PluginFormInterface::submitConfigurationForm
PluginFormInterface::validateConfigurationForm public function Form validation handler. 18
PluginInspectionInterface::getPluginDefinition public function Gets the definition of the plugin implementation. 4
PluginInspectionInterface::getPluginId public function Gets the plugin_id of the plugin instance. 2
RefinableCacheableDependencyInterface::addCacheableDependency public function Adds a dependency on an object: merges its cacheability metadata. 1
RefinableCacheableDependencyInterface::addCacheContexts public function Adds cache contexts. 1
RefinableCacheableDependencyInterface::addCacheTags public function Adds cache tags. 1
RefinableCacheableDependencyInterface::mergeCacheMaxAge public function Merges the maximum age (in seconds) with the existing maximum age. 1
VariantInterface::access public function Determines if this display variant is accessible. 1
VariantInterface::adminLabel public function Returns the admin-facing display variant label. 1
VariantInterface::getWeight public function Returns the weight of the display variant. 1
VariantInterface::id public function Returns the unique ID for the display variant. 1
VariantInterface::label public function Returns the user-facing display variant label. 1
VariantInterface::setWeight public function Sets the weight of the display variant. 1