You are here

function panelizer_panels_ipe_panels_display_presave in Panelizer 8.3

Same name and namespace in other branches
  1. 8.5 panelizer.module \panelizer_panels_ipe_panels_display_presave()
  2. 8.4 panelizer.module \panelizer_panels_ipe_panels_display_presave()

Implements hook_panels_ipe_panels_display_presave().

File

./panelizer.module, line 143
Hook implementations for the Panelizer module.

Code

function panelizer_panels_ipe_panels_display_presave(PanelsDisplayVariant $panels_display, array $layout_model) {
  if (empty($layout_model['panelizer_save_as'])) {
    return;
  }

  // See if the user requested changing the storage type.
  $current_storage = $panels_display
    ->getStorageType();
  $panelizer_save_as = $layout_model['panelizer_save_as'];
  if ($current_storage !== $panelizer_save_as) {
    $panelizer_entity = $layout_model['panelizer_entity'];

    // When actually saving, we want to use the real storage id for me the
    // Panelizer default.
    $panelizer_entity['panelizer_default_storage_id'] = $panelizer_entity['panelizer_default_real_storage_id'];

    // If we were custom and now we want to save to the default, we need to
    // save specially to the Panelizer field so that we can tell it we're on
    // a default.
    if ($panelizer_save_as == 'panelizer_default') {

      /** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager */
      $entity_type_manager = \Drupal::service("entity_type.manager");
      $storage = $entity_type_manager
        ->getStorage($panelizer_entity['entity_type_id']);
      $entity = $storage
        ->load($panelizer_entity['entity_id']);
      if ($entity instanceof FieldableEntityInterface) {

        /** @var \Drupal\panelizer\PanelizerInterface $panelizer */
        $panelizer = \Drupal::service('panelizer');
        list(, , , $default_name) = explode(':', $panelizer_entity['panelizer_default_storage_id']);
        $panelizer
          ->setPanelsDisplay($entity, $panelizer_entity['view_mode'], $default_name);
      }
    }

    // We need to generate a new UUID if we're creating a custom display.
    if ($current_storage == 'panelizer_default' && $panelizer_save_as == 'panelizer_field') {
      $configuration = $panels_display
        ->getConfiguration();
      $configuration['uuid'] = \Drupal::service('uuid')
        ->generate();
      $panels_display
        ->setConfiguration($configuration);
    }

    // Set the new storage information.
    $panels_display
      ->setStorage($panelizer_save_as, $panelizer_entity[$panelizer_save_as . '_storage_id']);
  }
}