You are here

public function Panelizer::getPanelsDisplay in Panelizer 8.5

Same name and namespace in other branches
  1. 8.3 src/Panelizer.php \Drupal\panelizer\Panelizer::getPanelsDisplay()
  2. 8.4 src/Panelizer.php \Drupal\panelizer\Panelizer::getPanelsDisplay()

Gets the Panels display for a given entity and view mode.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity.

string $view_mode: The entity view mode.

\Drupal\Core\Entity\Display\EntityViewDisplayInterface|NULL $display: If the caller already has the correct display, it can optionally be passed in here so the Panelizer service doesn't have to look it up; otherwise, this argument can bo omitted.

Return value

\Drupal\panels\Plugin\DisplayVariant\PanelsDisplayVariant|NULL The Panels display if panelized; NULL otherwise.

Overrides PanelizerInterface::getPanelsDisplay

File

src/Panelizer.php, line 212

Class

Panelizer
The Panelizer service.

Namespace

Drupal\panelizer

Code

public function getPanelsDisplay(FieldableEntityInterface $entity, $view_mode, EntityViewDisplayInterface $display = NULL) {
  $settings = $this
    ->getPanelizerSettings($entity
    ->getEntityTypeId(), $entity
    ->bundle(), $view_mode, $display);
  if (($settings['custom'] || $settings['allow']) && isset($entity->panelizer) && $entity->panelizer
    ->first()) {

    /** @var \Drupal\Core\Field\FieldItemInterface[] $values */
    $values = [];
    foreach ($entity->panelizer as $item) {
      $values[$item->view_mode] = $item;
    }
    if (isset($values[$view_mode])) {
      $panelizer_item = $values[$view_mode];

      // Check for a customized display first and use that if present.
      if (!empty($panelizer_item->panels_display)) {

        // @todo: validate schema after https://www.drupal.org/node/2392057 is fixed.
        return $this->panelsManager
          ->importDisplay($panelizer_item->panels_display, FALSE);
      }

      // If not customized, use the specified default.
      if (!empty($panelizer_item->default)) {

        // If we're using this magic key use the settings default.
        if ($panelizer_item->default == '__bundle_default__') {
          $default = $settings['default'];
        }
        else {
          $default = $panelizer_item->default;

          // Ensure the default still exists and if not fallback sanely.
          $displays = $this
            ->getDefaultPanelsDisplays($entity
            ->getEntityTypeId(), $entity
            ->bundle(), $view_mode);
          if (!isset($displays[$default])) {
            $default = $settings['default'];
          }
        }
        $panels_display = $this
          ->getDefaultPanelsDisplay($default, $entity
          ->getEntityTypeId(), $entity
          ->bundle(), $view_mode, $display);
        $this
          ->setCacheTags($panels_display, $entity
          ->getEntityTypeId(), $entity
          ->bundle(), $view_mode, $display, $default, $settings);
        return $panels_display;
      }
    }
  }

  // If the field has no input to give us, use the settings default.
  $panels_display = $this
    ->getDefaultPanelsDisplay($settings['default'], $entity
    ->getEntityTypeId(), $entity
    ->bundle(), $view_mode, $display);
  $this
    ->setCacheTags($panels_display, $entity
    ->getEntityTypeId(), $entity
    ->bundle(), $view_mode, $display, $settings['default'], $settings);
  return $panels_display;
}