You are here

public function ConfigPagesForm::form in Config Pages 8.3

Same name and namespace in other branches
  1. 8 src/ConfigPagesForm.php \Drupal\config_pages\ConfigPagesForm::form()
  2. 8.2 src/ConfigPagesForm.php \Drupal\config_pages\ConfigPagesForm::form()

Gets the actual form array to be built.

Overrides ContentEntityForm::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

File

src/ConfigPagesForm.php, line 144

Class

ConfigPagesForm
Form controller for the custom config page edit forms.

Namespace

Drupal\config_pages

Code

public function form(array $form, FormStateInterface $form_state) {
  $config_pages = $this->entity;
  $config_pages_type = $this->configPagesTypeStorage
    ->load($config_pages
    ->bundle());
  $form = parent::form($form, $form_state, $config_pages);
  $conditions['type'] = $config_pages
    ->bundle();
  $list = $this->entityTypeManager
    ->getStorage('config_pages')
    ->loadByProperties($conditions);

  // Show context message.
  $show_warning = $config_pages_type->context['show_warning'];
  $label = $config_pages_type
    ->getContextLabel();
  if (!empty($label) && $show_warning) {
    $this->messenger
      ->addWarning($this
      ->t('Please note that this Page is context sensitive, current context is %label', [
      '%label' => $label,
    ]));
  }
  if ($this->operation == 'edit') {
    $form['#title'] = $this
      ->t('Edit custom config page %label', [
      '%label' => $config_pages
        ->label(),
    ]);
  }

  // Override the default CSS class name, since the user-defined custom
  // config page type name in 'TYPE-config-page-form' potentially clashes
  // with third-party class names.
  $form['#attributes']['class'][0] = 'config-page-' . Html::getClass($config_pages
    ->bundle()) . '-form';

  // Create form elements to select another context.
  $links = $config_pages_type
    ->getContextLinks();
  foreach ($links as $group_id => $context_links) {

    // If no contextual links provided then just don't show fieldset.
    if (!count($context_links)) {
      continue;
    }

    // Fieldset to handle Context links.
    $form['context_selection_' . $group_id] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Choose @name context', [
        '@name' => $group_id,
      ]),
      '#weight' => -100,
    ];

    // Links to switch between available contexts.
    foreach ($context_links as $pos => $link) {
      if ($link['selected']) {
        $link['title'] = Markup::create('<strong>' . $link['title'] . '</strong>');
      }
      $form['context_selection_' . $group_id][$link['value']] = [
        '#type' => 'link',
        '#url' => $link['href'],
        '#title' => $link['title'],
        '#prefix' => $pos > 0 ? ' | ' : '',
      ];
    }
  }

  // Add context import fieldset if any CP exists at this moment.
  if (!$this->entity
    ->get('context')
    ->isEmpty()) {
    $options = [];
    foreach ($list as $id => $item) {

      // Build options list.
      if ($config_pages
        ->id() != $id) {
        $value = $item
          ->get('context')
          ->first()
          ->getValue();
        $params = unserialize($value['value']);
        $string = '';
        if (is_array($params)) {
          foreach ($params as $param) {
            foreach ($param as $name => $val) {
              $string .= $name . ' - ' . $val . ';';
            }
          }
          $options[$id] = $string;
        }
      }
    }

    // Show form if any data available.
    if (!empty($options)) {
      $form['other_context'] = [
        '#type' => 'details',
        '#tree' => TRUE,
        '#title' => t('Import'),
        '#weight' => 1000,
      ];
      $form['other_context']['list'] = [
        '#type' => 'select',
        '#options' => $options,
      ];
      $form['other_context']['submit'] = [
        '#type' => 'submit',
        '#value' => t('Import'),
        '#prefix' => '<div class="import-form-actions">',
        '#suffix' => '</div>',
        '#submit' => [
          '::configPagesImportValues',
        ],
      ];
    }
  }
  return $form;
}