You are here

public function SimpleSitemapEntityForm::form in Simple XML sitemap 4.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/SimpleSitemapEntityForm.php, line 45

Class

SimpleSitemapEntityForm
Class SimpleSitemapEntityForm

Namespace

Drupal\simple_sitemap\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(),
  ];
  $form['id'] = [
    '#type' => 'machine_name',
    '#default_value' => $this->entity
      ->id(),
    '#disabled' => !$this->entity
      ->isNew(),
    '#maxlength' => EntityTypeInterface::ID_MAX_LENGTH,
    '#required' => TRUE,
    '#machine_name' => [
      'exists' => '\\Drupal\\simple_sitemap\\Entity\\SimpleSitemap::load',
      'replace_pattern' => '[^a-z0-9-_.]+',
      'replace' => '-',
    ],
    '#description' => $this
      ->t('A unique name that will be part of the sitemap URL. Can only contain lowercase letters, numbers, dashes and underscores.'),
  ];
  $form['type'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Sitemap type'),
    '#options' => array_map(function ($sitemap_type) {
      return $sitemap_type
        ->label();
    }, SimpleSitemapType::loadMultiple()),
    '#default_value' => !$this->entity
      ->isNew() ? $this->entity
      ->getType()
      ->id() : NULL,
    '#required' => TRUE,
  ];
  $form['description'] = [
    '#type' => 'textarea',
    '#default_value' => $this->entity
      ->get('description'),
    '#title' => $this
      ->t('Description'),
  ];
  $form['actions'] = [
    '#type' => 'actions',
  ];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Save'),
  ];
  return $form;
}