You are here

public function ConfigPagesForm::form in Config Pages 8

Same name and namespace in other branches
  1. 8.3 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 106

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;
  $account = $this
    ->currentUser();
  $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) {
    drupal_set_message($this
      ->t('Please note that this Page is context sensitive, current context is %label', array(
      '%label' => $label,
    )), 'warning');
  }
  if ($this->operation == 'edit') {
    $form['#title'] = $this
      ->t('Edit custom config page %label', array(
      '%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';

  // 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']);
        $params = array_shift($params);
        $string = '';
        if (is_array($params)) {
          foreach ($params 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'),
      ];
      $form['other_context']['list'] = [
        '#type' => 'select',
        '#options' => $options,
      ];
      $form['other_context']['submit'] = [
        '#type' => 'submit',
        '#value' => t('Import'),
        '#prefix' => '<div class="imort-form-actions">',
        '#suffix' => '</div>',
        '#submit' => array(
          '::configPagesImportValues',
        ),
      ];
    }
  }
  return $form;
}