You are here

function panelizer_panels_build_alter in Panelizer 8.3

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

Implements hook_panels_build_alter().

File

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

Code

function panelizer_panels_build_alter(&$build, PanelsDisplayVariant $panels_display) {
  $builder = $panels_display
    ->getBuilder();
  $storage_type = $panels_display
    ->getStorageType();
  $is_panelizer = $builder
    ->getPluginId() == 'ipe' && in_array($storage_type, [
    'panelizer_default',
    'panelizer_field',
  ]) && isset($build['#attached']) && isset($build['#attached']['library']) && in_array('panels_ipe/panels_ipe', $build['#attached']['library']);

  // Add our Javascript customizations for the IPE.
  if ($is_panelizer) {
    $build['#attached']['library'][] = 'panelizer/panels_ipe';

    /** @var \Drupal\Core\Entity\EntityInterface $entity */
    $entity = $panels_display
      ->getContexts()['@panelizer.entity_context:entity']
      ->getContextValue();
    $revision_id = $entity instanceof RevisionableInterface && $entity
      ->getEntityType()
      ->isRevisionable() && !$entity
      ->isDefaultRevision() ? $entity
      ->getRevisionId() : NULL;
    list(, , $view_mode) = explode(':', $panels_display
      ->getStorageId());

    // Get the default storage id, if we're looking at a panelizer default or
    // the panelizer field contains a revision.
    if (sizeof(explode(':', $panels_display
      ->getStorageId())) == 4) {
      list(, , , $default) = explode(':', $panels_display
        ->getStorageId());
    }
    else {
      $default = NULL;
    }
    if ($panels_display
      ->getStorageType() == 'panelizer_field') {
      $panelizer_default_storage_id = rtrim(implode(':', [
        $entity
          ->getEntityTypeId(),
        $entity
          ->bundle(),
        $view_mode,
        $default,
      ]), ':');
    }
    else {
      $panelizer_default_storage_id = $panels_display
        ->getStorageId();
    }

    // Get the special, internal default storage id that includes the entity id,
    // which will allow us to correctly set the contexts on the Panels display.
    $panelizer_default_internal_storage_id = '*' . rtrim(implode(':', [
      $entity
        ->getEntityTypeId(),
      $entity
        ->id(),
      $view_mode,
      $default,
    ]), ':');

    // Get the custom storage id (omitting revision id if this is the default
    // revision).
    $panelizer_field_storage_id_parts = [
      $entity
        ->getEntityTypeId(),
      $entity
        ->id(),
      $view_mode,
    ];
    if ($revision_id) {
      $panelizer_field_storage_id_parts[] = $revision_id;
    }
    $panelizer_field_storage_id = implode(':', $panelizer_field_storage_id_parts);

    /** @var \Drupal\panelizer\PanelizerInterface $panelizer */
    $panelizer = \Drupal::service('panelizer');
    $build['#attached']['drupalSettings']['panelizer']['entity'] = [
      'entity_type_id' => $entity
        ->getEntityTypeId(),
      'entity_id' => $entity
        ->id(),
      'view_mode' => $view_mode,
      'revision_id' => $revision_id,
      'panelizer_default_storage_id' => $panelizer
        ->hasDefaultPermission('change content', $entity
        ->getEntityTypeId(), $entity
        ->bundle(), $view_mode, $default) ? $panelizer_default_internal_storage_id : FALSE,
      'panelizer_field_storage_id' => $panelizer
        ->hasEntityPermission('change content', $entity, $view_mode) ? $panelizer_field_storage_id : FALSE,
      'panelizer_default_real_storage_id' => $panelizer_default_storage_id,
    ];

    // Whether or not the current user has access to the "revert to default"
    // action in the IPE; any user with the 'administer panelizer' will also
    // have access.
    $build['#attached']['drupalSettings']['panelizer']['user_permission']['revert'] = $panelizer
      ->hasEntityPermission('revert to default', $entity, $view_mode);

    // Whether or not the current user has access to the "set as default" action
    // in the IPE; any user with the 'administer panelizer' will also have
    // access.
    $user = \Drupal::currentUser();
    $build['#attached']['drupalSettings']['panelizer']['user_permission']['save_default'] = $user
      ->hasPermission('set panelizer default') || $user
      ->hasPermission('administer panelizer');
    if ($panels_display
      ->getStorageType() == 'panelizer_field') {

      // If using panelizer_field, we change the storage id to match what we put
      // together here because it'll have the revision id omitted in the right
      // situation.
      $build['#attached']['drupalSettings']['panels_ipe']['panels_display']['storage_id'] = $panelizer_field_storage_id;
    }
    else {

      // If using panelizer_default, we need to switch to a the special,
      // internal storage id.
      $build['#attached']['drupalSettings']['panels_ipe']['panels_display']['storage_id'] = $panelizer_default_internal_storage_id;
    }
  }
}