You are here

class PageGeneralForm in Page Manager 8

Same name and namespace in other branches
  1. 8.4 page_manager_ui/src/Form/PageGeneralForm.php \Drupal\page_manager_ui\Form\PageGeneralForm

Hierarchy

Expanded class hierarchy of PageGeneralForm

1 file declares its use of PageGeneralForm
PageWizardBase.php in page_manager_ui/src/Wizard/PageWizardBase.php
Contains \Drupal\page_manager_ui\Wizard\PageWizardBase.

File

page_manager_ui/src/Form/PageGeneralForm.php, line 17
Contains \Drupal\page_manager_ui\Form\PageGeneralForm.

Namespace

Drupal\page_manager_ui\Form
View source
class PageGeneralForm extends FormBase {

  /**
   * The variant manager.
   *
   * @var \Drupal\Core\Display\VariantManager
   */
  protected $variantManager;

  /**
   * The entity query factory.
   *
   * @var \Drupal\Core\Entity\Query\QueryFactory
   */
  protected $entityQuery;

  /**
   * Constructs a new PageGeneralForm.
   *
   * @param \Drupal\Core\Display\VariantManager $variant_manager
   *   The variant manager.
   * @param \Drupal\Core\Entity\Query\QueryFactory $entity_query
   *   The entity query factory.
   */
  public function __construct(VariantManager $variant_manager, QueryFactory $entity_query) {
    $this->variantManager = $variant_manager;
    $this->entityQuery = $entity_query;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('plugin.manager.display_variant'), $container
      ->get('entity.query'));
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'page_manager_general_form';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $cached_values = $form_state
      ->getTemporaryValue('wizard');

    /** @var $page \Drupal\page_manager\Entity\Page */
    $page = $cached_values['page'];
    $form['description'] = [
      '#type' => 'textarea',
      '#title' => $this
        ->t('Administrative description'),
      '#default_value' => $page
        ->getDescription(),
    ];
    $form['path'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Path'),
      '#maxlength' => 255,
      '#default_value' => $page
        ->getPath(),
      '#required' => TRUE,
      '#element_validate' => [
        [
          $this,
          'validatePath',
        ],
      ],
    ];
    $form['use_admin_theme'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Use admin theme'),
      '#default_value' => $page
        ->usesAdminTheme(),
    ];
    if ($page
      ->isNew()) {
      $variant_plugin_options = [];
      foreach ($this->variantManager
        ->getDefinitions() as $plugin_id => $definition) {

        // The following two variants are provided by Drupal Core. They are not
        // configurable and therefore not compatible with Page Manager but have
        // similar and confusing labels. Skip them so that they are not shown in
        // the UI.
        if (in_array($plugin_id, [
          'simple_page',
          'block_page',
        ])) {
          continue;
        }
        $variant_plugin_options[$plugin_id] = $definition['admin_label'];
      }
      $form['variant_plugin_id'] = [
        '#title' => $this
          ->t('Variant type'),
        '#type' => 'select',
        '#options' => $variant_plugin_options,
        '#default_value' => !empty($cached_values['variant_plugin_id']) ? $cached_values['variant_plugin_id'] : '',
      ];
      $form['wizard_options'] = [
        '#type' => 'checkboxes',
        '#title' => $this
          ->t('Optional features'),
        '#description' => $this
          ->t('Check any optional features you need to be presented with forms for configuring them. If you do not check them here you will still be able to utilize these features once the new page is created. If you are not sure, leave these unchecked.'),
        '#options' => [
          'access' => $this
            ->t('Page access'),
          'contexts' => $this
            ->t('Variant contexts'),
          'selection' => $this
            ->t('Variant selection criteria'),
        ],
        '#default_value' => !empty($cached_values['wizard_options']) ? $cached_values['wizard_options'] : [],
      ];
    }
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $cached_values = $form_state
      ->getTemporaryValue('wizard');

    /** @var $page \Drupal\page_manager\Entity\Page */
    $page = $cached_values['page'];
    $page
      ->set('description', $form_state
      ->getValue('description'));
    $page
      ->set('path', $form_state
      ->getValue('path'));
    $page
      ->set('use_admin_theme', $form_state
      ->getValue('use_admin_theme'));
    if ($page
      ->isNew()) {
      $page
        ->set('id', $form_state
        ->getValue('id'));
      $page
        ->set('label', $form_state
        ->getValue('label'));
      if (empty($cached_values['variant_plugin_id'])) {
        $variant_plugin_id = $cached_values['variant_plugin_id'] = $form_state
          ->getValue('variant_plugin_id');

        /* @var \Drupal\page_manager\PageVariantInterface $page_variant */
        $page_variant = \Drupal::entityManager()
          ->getStorage('page_variant')
          ->create([
          'variant' => $form_state
            ->getValue('variant_plugin_id'),
          'page' => $page
            ->id(),
          'id' => "{$page->id()}-{$variant_plugin_id}-0",
          'label' => $form['variant_plugin_id']['#options'][$variant_plugin_id],
        ]);
        $page_variant
          ->setPageEntity($page);
        $page
          ->addVariant($page_variant);
        $cached_values['page_variant'] = $page_variant;
      }
      if ($cached_values['variant_plugin_id'] != $form_state
        ->getValue('variant_plugin_id') && !empty($cached_values['page_variant'])) {
        $page_variant = $cached_values['page_variant'];

        /** @var $page_variant \Drupal\page_manager\Entity\PageVariant */
        $page_variant
          ->set('variant', $form_state
          ->getValue('variant_plugin_id'));
        $page_variant
          ->set('variant_settings', []);
        $cached_values['variant_plugin_id'] = $form_state
          ->getValue('variant_plugin_id');
      }
      $cached_values['wizard_options'] = $form_state
        ->getValue('wizard_options');
      $form_state
        ->setTemporaryValue('wizard', $cached_values);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function validatePath(&$element, FormStateInterface $form_state) {
    $cached_values = $form_state
      ->getTemporaryValue('wizard');

    /** @var $page \Drupal\page_manager\Entity\Page */
    $page = $cached_values['page'];

    // Ensure the path has a leading slash.
    $value = '/' . trim($element['#value'], '/');
    $form_state
      ->setValueForElement($element, $value);

    // Ensure each path is unique.
    $path_query = $this->entityQuery
      ->get('page')
      ->condition('path', $value);
    if (!$page
      ->isNew()) {
      $path_query
        ->condition('id', $page
        ->id(), '<>');
    }
    $path = $path_query
      ->execute();
    if ($path) {
      $form_state
        ->setErrorByName('path', $this
        ->t('The page path must be unique.'));
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 62
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PageGeneralForm::$entityQuery protected property The entity query factory.
PageGeneralForm::$variantManager protected property The variant manager.
PageGeneralForm::buildForm public function Form constructor. Overrides FormInterface::buildForm
PageGeneralForm::create public static function Instantiates a new instance of this class. Overrides FormBase::create
PageGeneralForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
PageGeneralForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
PageGeneralForm::validatePath public function
PageGeneralForm::__construct public function Constructs a new PageGeneralForm.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.