You are here

function lightning_layout_tweak_panelizer_ui in Lightning Layout 8

Tweaks the Panelizer stuff on an entity view display form.

Parameters

array $element: The form element containing Panelizer's entity view display options.

\Drupal\Core\Form\FormStateInterface $form_state: The current form state.

Return value

array The processed element.

1 string reference to 'lightning_layout_tweak_panelizer_ui'
lightning_layout_form_entity_view_display_edit_form_alter in ./lightning_layout.module
Implements hook_form_FORM_ID_alter().

File

./lightning_layout.module, line 104
Contains layout functionality for Lightning.

Code

function lightning_layout_tweak_panelizer_ui(array $element, FormStateInterface $form_state) {

  /** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display */
  $display = $form_state
    ->getFormObject()
    ->getEntity();
  $panelizer_enabled = $display
    ->getThirdPartySetting('panelizer', 'enable', FALSE);

  // Check if this display is for an internal view mode.
  $view_mode = EntityViewMode::load($display
    ->getTargetEntityTypeId() . '.' . $display
    ->getMode());
  if ($view_mode) {
    $internal = $view_mode
      ->getThirdPartySetting('lightning_core', 'internal');
    if ($internal) {

      // If it's not already applied, don't allow Panelizer.
      $element['panelizer']['#access'] = $panelizer_enabled;

      // Inform the user what's up.
      \Drupal::messenger()
        ->addWarning(t('This display is internal and will not be seen by normal users.'));
    }
  }

  // If Panelizer isn't enabled, there's nothing else to do.
  if (empty($panelizer_enabled)) {
    return $element;
  }

  // Don't show the table caption.
  // TODO: Is there an accessible way to hide this?
  unset($element['panelizer']['displays']['#caption']);
  $route_parameters = \Drupal::routeMatch()
    ->getParameters()
    ->all();
  $route_parameters = array_filter($route_parameters, 'is_scalar');

  // We got rid of the local action for this, so jury-rig a new local action
  // that we can mix in with the rest of the UI elements.
  // See lightning_layout_menu_local_actions_alter().
  $element['panelizer']['add_link'] = [
    '#theme' => 'menu_local_action',
    '#link' => [
      'title' => t('Create a layout'),
      'url' => Url::fromRoute('panelizer.wizard.add', $route_parameters),
    ],
    // @TODO: Use a theme wrapper if possible.
    '#prefix' => '<ul class="action-links">',
    '#suffix' => '</ul>',
  ];
  Element::order($element['panelizer'], [
    'enable',
    'options',
    'add_link',
    'displays',
  ]);
  return $element;
}