You are here

page_manager_ui.module in Page Manager 8.4

Same filename and directory in other branches
  1. 8 page_manager_ui/page_manager_ui.module

Provides a UI for Page Manager.

File

page_manager_ui/page_manager_ui.module
View source
<?php

/**
 * @file
 * Provides a UI for Page Manager.
 */
use Drupal\Core\Url;
use Drupal\page_manager_ui\Entity\PageListBuilder;
use Drupal\page_manager_ui\Form\PageDeleteForm;
use Drupal\page_manager_ui\ConfigTranslation\PageConfigMapper;
use Drupal\page_manager_ui\ConfigTranslation\PageVariantConfigMapper;
use Drupal\page_manager_ui\Wizard\PageAddWizard;
use Drupal\page_manager_ui\Wizard\PageEditWizard;
use Drupal\page_manager_ui\Wizard\PageVariantAddWizard;

/**
 * Implements hook_entity_type_build().
 */
function page_manager_ui_entity_type_build(array &$entity_types) {

  /** @var $entity_types \Drupal\Core\Entity\EntityTypeInterface[] */
  if (isset($entity_types['page'])) {
    $entity_types['page']
      ->setListBuilderClass(PageListBuilder::class)
      ->setFormClass('delete', PageDeleteForm::class)
      ->setLinkTemplate('collection', '/admin/structure/page_manager')
      ->setLinkTemplate('add-form', '/admin/structure/page_manager/add')
      ->setLinkTemplate('edit-form', '/admin/structure/page_manager/manage/{machine_name}/{step}')
      ->setLinkTemplate('delete-form', '/admin/structure/page_manager/manage/{page}/delete')
      ->setLinkTemplate('enable', '/admin/structure/page_manager/manage/{page}/enable')
      ->setLinkTemplate('disable', '/admin/structure/page_manager/manage/{page}/disable')
      ->setHandlerClass('wizard', [
      'add' => PageAddWizard::class,
      'edit' => PageEditWizard::class,
    ]);
  }
  if (isset($entity_types['page_variant'])) {
    $entity_types['page_variant']
      ->setLinkTemplate('edit-form', '/admin/structure/page_manager/manage/{machine_name}/{step}')
      ->setHandlerClass('wizard', [
      'add_variant' => PageVariantAddWizard::class,
    ]);
  }
}

/**
 * Implements hook_entity_type_alter().
 */
function page_manager_ui_entity_type_alter(array &$entity_types) {

  // Change the URL for page config translation overview to outside the wizard.
  if ($entity_types['page']
    ->hasLinkTemplate('config-translation-overview')) {
    $entity_types['page']
      ->setLinkTemplate('config-translation-overview', str_replace('manage/{machine_name}/{step}', '{page}', $entity_types['page']
      ->getLinkTemplate('config-translation-overview')));
  }

  // Change the URL for page variant config translation overview to outside the
  // wizard.
  if ($entity_types['page_variant']
    ->hasLinkTemplate('config-translation-overview')) {
    $entity_types['page_variant']
      ->setLinkTemplate('config-translation-overview', str_replace('manage/{machine_name}/{step}', '{page}/{page_variant}', $entity_types['page_variant']
      ->getLinkTemplate('config-translation-overview')));
  }
}

/**
 * Implements hook_theme().
 */
function page_manager_ui_theme() {
  return [
    'page_manager_wizard_form' => [
      'render element' => 'form',
    ],
    'page_manager_wizard_tree' => [
      'variables' => [
        'wizard' => NULL,
        'cached_values' => [],
        'tree' => [],
        'divider' => ' » ',
        'step' => NULL,
      ],
    ],
  ];
}

/**
 * Preprocess function for page-manager-wizard-tree.html.twig.
 */
function template_preprocess_page_manager_wizard_tree(&$variables) {

  /** @var $wizard \Drupal\ctools\Wizard\FormWizardInterface|\Drupal\ctools\Wizard\EntityFormWizardInterface */
  $wizard = $variables['wizard'];
  $cached_values = $variables['cached_values'];
  $tree = $variables['tree'];
  $variables['step'] = $wizard
    ->getStep($cached_values);
  foreach ($wizard
    ->getOperations($cached_values) as $step => $operation) {
    $parameters = $wizard
      ->getNextParameters($cached_values);

    // Override step to be the step we want.
    $parameters['step'] = $step;

    // Fill in parents if there are breadcrumbs.
    $parent =& $tree;
    if (isset($operation['breadcrumbs'])) {
      foreach ($operation['breadcrumbs'] as $breadcrumb) {
        $breadcrumb_string = (string) $breadcrumb;
        if (!isset($parent[$breadcrumb_string])) {
          $parent[$breadcrumb_string] = [
            'title' => $breadcrumb,
            'children' => [],
          ];
        }
        $parent =& $parent[$breadcrumb_string]['children'];
      }
    }
    $parent[$step] = [
      'title' => !empty($operation['title']) ? $operation['title'] : '',
      'url' => new Url($wizard
        ->getRouteName(), $parameters),
      'step' => $step,
    ];
  }
  $variables['tree'] = $tree;
}

/**
 * Implements hook_config_translation_info_alter().
 */
function page_manager_ui_config_translation_info_alter(&$info) {

  // Alter page and page variant config translation classes.
  if (isset($info['page'])) {
    $info['page']['class'] = PageConfigMapper::class;
  }
  if (isset($info['page_variant'])) {
    $info['page_variant']['class'] = PageVariantConfigMapper::class;
    $info['page_variant']['base_route_name'] = 'entity.page.edit_form';
  }
}

/**
 * Implements hook_local_tasks_alter().
 */
function page_manager_ui_local_tasks_alter(&$local_tasks) {

  // Remove local tasks for page and page variant config translation overview
  // routes.
  unset($local_tasks['config_translation.local_tasks:entity.page.config_translation_overview']);
  unset($local_tasks['config_translation.local_tasks:entity.page_variant.config_translation_overview']);
}

/**
 * Implements hook_theme_registry_alter().
 *
 * @todo Refactor/remove if https://www.drupal.org/project/drupal/issues/3005403 lands.
 */
function page_manager_ui_theme_registry_alter(&$theme_registry) {

  // Seven removes all block contextual links via a preprocess.
  // Layout builder variants require contextual links to configure blocks.
  // @see https://www.drupal.org/project/drupal/issues/2487025
  if (!empty($theme_registry['block']['preprocess functions'])) {
    $preprocess_functions =& $theme_registry['block']['preprocess functions'];

    // Remove seven's implementation.
    if ($index = array_search('seven_preprocess_block', $preprocess_functions)) {
      unset($preprocess_functions[$index]);
    }
  }
}

/**
 * Implements hook_preprocess_HOOK().
 *
 * @todo Refactor/remove if https://www.drupal.org/project/drupal/issues/3005403 lands.
 */
function page_manager_ui_preprocess_block(&$variables) {
  if (Drupal::theme()
    ->getActiveTheme()
    ->getName() === 'seven') {

    // If active theme is seven and the block has layout_builder contextual
    // links, do nothing.
    if (isset($variables['elements']['#contextual_links']['layout_builder_block'])) {
      return;
    }

    // Fallback to seven.
    seven_preprocess_block($variables);
  }
}