You are here

public function PatternEditForm::buildForm in Pathauto 8

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

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

Return value

array The form structure.

Overrides EntityForm::buildForm

File

src/Form/PatternEditForm.php, line 85

Class

PatternEditForm
Edit form for pathauto patterns.

Namespace

Drupal\pathauto\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $options = [];
  foreach ($this->manager
    ->getVisibleDefinitions() as $plugin_id => $plugin_definition) {
    $options[$plugin_id] = $plugin_definition['label'];
  }
  $form['type'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Pattern type'),
    '#default_value' => $this->entity
      ->getType(),
    '#options' => $options,
    '#required' => TRUE,
    '#limit_validation_errors' => [
      [
        'type',
      ],
    ],
    '#submit' => [
      '::submitSelectType',
    ],
    '#executes_submit_callback' => TRUE,
    '#ajax' => [
      'callback' => '::ajaxReplacePatternForm',
      'wrapper' => 'pathauto-pattern',
      'method' => 'replace',
    ],
  ];
  $form['pattern_container'] = [
    '#type' => 'container',
    '#prefix' => '<div id="pathauto-pattern">',
    '#suffix' => '</div>',
  ];

  // if there is no type yet, stop here.
  if ($this->entity
    ->getType()) {
    $alias_type = $this->entity
      ->getAliasType();
    $form['pattern_container']['pattern'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Path pattern'),
      '#default_value' => $this->entity
        ->getPattern(),
      '#size' => 65,
      '#maxlength' => 1280,
      '#element_validate' => [
        'token_element_validate',
        'pathauto_pattern_validate',
      ],
      '#after_build' => [
        'token_element_validate',
      ],
      '#token_types' => $alias_type
        ->getTokenTypes(),
      '#min_tokens' => 1,
      '#required' => TRUE,
    ];

    // Show the token help relevant to this pattern type.
    $form['pattern_container']['token_help'] = [
      '#theme' => 'token_tree_link',
      '#token_types' => $alias_type
        ->getTokenTypes(),
    ];

    // Expose bundle and language conditions.
    if ($alias_type
      ->getDerivativeId() && ($entity_type = $this->entityTypeManager
      ->getDefinition($alias_type
      ->getDerivativeId()))) {
      $default_bundles = [];
      $default_languages = [];
      foreach ($this->entity
        ->getSelectionConditions() as $condition) {
        if (in_array($condition
          ->getPluginId(), [
          'entity_bundle:' . $entity_type
            ->id(),
          'node_type',
        ])) {
          $default_bundles = $condition
            ->getConfiguration()['bundles'];
        }
        elseif ($condition
          ->getPluginId() == 'language') {
          $default_languages = $condition
            ->getConfiguration()['langcodes'];
        }
      }
      if ($entity_type
        ->hasKey('bundle') && ($bundles = $this->entityTypeBundleInfo
        ->getBundleInfo($entity_type
        ->id()))) {
        $bundle_options = [];
        foreach ($bundles as $id => $info) {
          $bundle_options[$id] = $info['label'];
        }
        $form['pattern_container']['bundles'] = [
          '#title' => $entity_type
            ->getBundleLabel(),
          '#type' => 'checkboxes',
          '#options' => $bundle_options,
          '#default_value' => $default_bundles,
          '#description' => $this
            ->t('Check to which types this pattern should be applied. Leave empty to allow any.'),
        ];
      }
      if ($this->languageManager
        ->isMultilingual() && $entity_type
        ->isTranslatable()) {
        $language_options = [];
        foreach ($this->languageManager
          ->getLanguages() as $id => $language) {
          $language_options[$id] = $language
            ->getName();
        }
        $form['pattern_container']['languages'] = [
          '#title' => $this
            ->t('Languages'),
          '#type' => 'checkboxes',
          '#options' => $language_options,
          '#default_value' => $default_languages,
          '#description' => $this
            ->t('Check to which languages this pattern should be applied. Leave empty to allow any.'),
        ];
      }
    }
  }
  $form['label'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Label'),
    '#maxlength' => 255,
    '#default_value' => $this->entity
      ->label(),
    '#required' => TRUE,
    '#description' => $this
      ->t('A short name to help you identify this pattern in the patterns list.'),
  ];
  $form['id'] = [
    '#type' => 'machine_name',
    '#title' => $this
      ->t('ID'),
    '#maxlength' => 255,
    '#default_value' => $this->entity
      ->id(),
    '#required' => TRUE,
    '#disabled' => !$this->entity
      ->isNew(),
    '#machine_name' => [
      'exists' => 'Drupal\\pathauto\\Entity\\PathautoPattern::load',
    ],
  ];
  $form['status'] = [
    '#title' => $this
      ->t('Enabled'),
    '#type' => 'checkbox',
    '#default_value' => $this->entity
      ->status(),
  ];
  return parent::buildForm($form, $form_state);
}