You are here

public function PanelizerFieldPanelsStorage::save in Panelizer 8.4

Same name and namespace in other branches
  1. 8.5 src/Plugin/PanelsStorage/PanelizerFieldPanelsStorage.php \Drupal\panelizer\Plugin\PanelsStorage\PanelizerFieldPanelsStorage::save()
  2. 8.3 src/Plugin/PanelsStorage/PanelizerFieldPanelsStorage.php \Drupal\panelizer\Plugin\PanelsStorage\PanelizerFieldPanelsStorage::save()

File

src/Plugin/PanelsStorage/PanelizerFieldPanelsStorage.php, line 128

Class

PanelizerFieldPanelsStorage
Panels storage service that stores Panels displays in the Panelizer field.

Namespace

Drupal\panelizer\Plugin\PanelsStorage

Code

public function save(PanelsDisplayVariant $panels_display) {
  $id = $panels_display
    ->getStorageId();
  if ($entity = $this
    ->loadEntity($id)) {
    list(, , $view_mode) = explode(':', $id);

    // If we're dealing with an entity that has a documented default, we
    // don't want to lose that information when we save our customizations.
    // This enables us to revert to the correct default at a later date.
    if ($entity instanceof FieldableEntityInterface) {
      $default = NULL;
      if ($entity
        ->hasField('panelizer') && $entity->panelizer
        ->first()) {
        foreach ($entity->panelizer as $item) {
          if ($item->view_mode == $view_mode) {
            $default = $item->default;
            break;
          }
        }
      }
      try {
        $this->panelizer
          ->setPanelsDisplay($entity, $view_mode, $default, $panels_display);
      } catch (PanelizerException $e) {

        // Translate to expected exception type.
        throw new \Exception($e
          ->getMessage());
      }
    }
  }
  else {
    throw new \Exception("Couldn't find entity to store Panels display on");
  }
}