You are here

public function CustomBreadcrumbsForm::form in Custom Breadcrumbs 1.x

Gets the actual form array to be built.

Overrides EntityForm::form

See also

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

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

File

src/Form/CustomBreadcrumbsForm.php, line 60

Class

CustomBreadcrumbsForm
Custom breadcrumbs form.

Namespace

Drupal\custom_breadcrumbs\Form

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);
  $form['label'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Label'),
    '#maxlength' => 255,
    '#default_value' => $this->entity
      ->label(),
    '#description' => $this
      ->t('Label for the custom breadcrumb.'),
    '#required' => TRUE,
  ];
  $form['id'] = [
    '#type' => 'machine_name',
    '#default_value' => $this->entity
      ->id(),
    '#machine_name' => [
      'exists' => '\\Drupal\\custom_breadcrumbs\\Entity\\CustomBreadcrumbs::load',
    ],
    '#disabled' => !$this->entity
      ->isNew(),
  ];
  $form['status'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Enabled'),
    '#default_value' => $this->entity
      ->status(),
  ];
  $form['description'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Description'),
    '#default_value' => $this->entity
      ->get('description'),
    '#description' => $this
      ->t('Description of the custom breadcrumb.'),
  ];
  $form['type'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Type of breadcrumb'),
    '#options' => [
      1 => $this
        ->t('Content entity'),
      2 => $this
        ->t('Path'),
    ],
    '#requires' => TRUE,
    '#default_value' => $this->entity
      ->get('type'),
  ];
  $entity = $this->entity
    ->get('entityType');
  $entity = $form_state
    ->hasValue('entityType') ? $form_state
    ->getValue('entityType') : $entity;
  $form['entityType'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Entity type'),
    '#default_value' => $entity,
    '#description' => $this
      ->t('Select your entity type.'),
    '#options' => $this
      ->getEntityTypes(),
    '#empty_value' => '_none',
    '#ajax' => [
      'callback' => [
        $this,
        'ajaxCallback',
      ],
      'wrapper' => 'entity_bundle_configuration',
      'method' => 'replace',
      'effect' => 'fade',
    ],
    '#states' => [
      'visible' => [
        ':input[name="type"]' => [
          'value' => '1',
        ],
      ],
    ],
  ];
  $form['entityBundle'] = [
    '#prefix' => '<div id="entity_bundle_configuration">',
    '#suffix' => '</div>',
    '#type' => 'select',
    '#title' => $this
      ->t('Entity bundle'),
    '#default_value' => $this->entity
      ->get('entityBundle'),
    '#description' => $this
      ->t('Select your entity bundle.'),
    '#options' => $this
      ->getEntityBundle($entity),
    '#empty_value' => '_none',
    '#states' => [
      'visible' => [
        ':input[name="type"]' => [
          'value' => '1',
        ],
      ],
    ],
  ];
  $form['language'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Language'),
    '#default_value' => $this->entity
      ->get('language'),
    '#description' => $this
      ->t('Select language'),
    '#options' => $this
      ->getAvailableLanguages(),
  ];
  $form['pathPattern'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Path pattern'),
    '#default_value' => $this->entity
      ->get('pathPattern'),
    '#description' => $this
      ->t('A set of patterns separated by a newline. @front_key@ is used to front page. The \'*\' character is a wildcard. An example path is /admin/* for every admin pages.', [
      '@front_key@' => '<front>',
    ]),
    '#states' => [
      'visible' => [
        ':input[name="type"]' => [
          'value' => '2',
        ],
      ],
    ],
  ];
  $form['breadcrumbPaths'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Breadcrumb paths'),
    '#default_value' => $this->entity
      ->get('breadcrumbPaths'),
    '#required' => TRUE,
    '#description' => $this
      ->t('One url per line, you can use <a href="@token">Token</a> module. Url must start from "/". Use @nolink_key if you don\'t want to set a link for the respective title.', [
      '@token' => 'https://www.drupal.org/project/token',
      '@nolink_key' => '<nolink>',
    ]),
  ];
  $form['breadcrumbTitles'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Breadcrumb titles'),
    '#default_value' => $this->entity
      ->get('breadcrumbTitles'),
    '#required' => TRUE,
    '#description' => $this
      ->t('One title per line, you can use <a href="@token">Token</a> module.', [
      '@token' => 'https://www.drupal.org/project/token',
    ]),
  ];
  $form['extraCacheContexts'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Extra cache contexts'),
    '#default_value' => $this->entity
      ->get('extraCacheContexts'),
    '#description' => $this
      ->t('You can define an extra cache contexts for example for curent request query "url.query_args:search".'),
  ];
  $form['token_tree'] = [
    '#theme' => 'token_tree_link',
    '#token_types' => [
      self::getTokenEntity($entity),
    ],
    '#show_restricted' => TRUE,
    '#weight' => 90,
  ];
  $form['token_details'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Custom breadcrumbs extra vars'),
    '#open' => FALSE,
  ];
  $form['token_details']['vars'] = [
    '#theme' => 'item_list',
    '#items' => [
      $this
        ->t('&ltnolink&gt - adds ability to create crumb without url'),
      $this
        ->t('&ltterm_hierarchy:field_name&gt - taxonomy term field with hierarchy'),
    ],
  ];
  return $form;
}