You are here

public function Panelizer::setPanelsDisplay in Panelizer 8.4

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

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

Parameters

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

string $view_mode: The entity view mode.

string|NULL $default: The name of the default if setting to a default; otherwise NULL.

\Drupal\panels\Plugin\DisplayVariant\PanelsDisplayVariant|NULL $panels_display: The Panels display if this is an override; otherwise NULL.

Throws

\Drupal\panelizer\Exception\PanelizerException When custom overrides aren't enabled on this entity, bundle and view mode.

Overrides PanelizerInterface::setPanelsDisplay

File

src/Panelizer.php, line 290

Class

Panelizer
The Panelizer service.

Namespace

Drupal\panelizer

Code

public function setPanelsDisplay(FieldableEntityInterface $entity, $view_mode, $default, PanelsDisplayVariant $panels_display = NULL) {
  $settings = $this
    ->getPanelizerSettings($entity
    ->getEntityTypeId(), $entity
    ->bundle(), $view_mode);
  if (($settings['custom'] || $settings['allow']) && isset($entity->panelizer)) {
    $panelizer_item = NULL;

    /** @var \Drupal\Core\Field\FieldItemInterface $item */
    foreach ($entity->panelizer as $item) {
      if ($item->view_mode == $view_mode) {
        $panelizer_item = $item;
        break;
      }
    }
    if (!$panelizer_item) {
      $panelizer_item = $this->fieldTypeManager
        ->createFieldItem($entity->panelizer, count($entity->panelizer));
      $panelizer_item->view_mode = $view_mode;
    }

    // Note: We don't call $panels_display->setStorage() here because it will
    // be set internally by PanelizerFieldType::postSave() which will know
    // the real revision ID of the newly saved entity.
    $panelizer_item->panels_display = $panels_display ? $this->panelsManager
      ->exportDisplay($panels_display) : [];
    $panelizer_item->default = $default;

    // Create a new revision if possible.
    if ($entity instanceof RevisionableInterface && $entity
      ->getEntityType()
      ->isRevisionable()) {
      if ($entity
        ->isDefaultRevision()) {
        $entity
          ->setNewRevision(TRUE);
      }
    }

    // Updates the changed time of the entity, if necessary.
    if ($entity
      ->getEntityType()
      ->entityClassImplements(EntityChangedInterface::class)) {
      $request_time = \Drupal::time()
        ->getRequestTime();
      $entity
        ->setChangedTime($request_time);
    }
    $entity->panelizer[$panelizer_item
      ->getName()] = $panelizer_item;
    $entity
      ->save();
  }
  else {
    throw new PanelizerException("Custom overrides not enabled on this entity, bundle and view mode");
  }
}