You are here

public function XmlSitemapCustomEditForm::buildForm in XML sitemap 8

Same name and namespace in other branches
  1. 2.x xmlsitemap_custom/src/Form/XmlSitemapCustomEditForm.php \Drupal\xmlsitemap_custom\Form\XmlSitemapCustomEditForm::buildForm()

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 FormInterface::buildForm

File

xmlsitemap_custom/src/Form/XmlSitemapCustomEditForm.php, line 100

Class

XmlSitemapCustomEditForm
Provides a form for editing a custom link.

Namespace

Drupal\xmlsitemap_custom\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $link = '') {
  if (!($custom_link = $this->linkStorage
    ->load('custom', $link))) {
    $this
      ->messenger()
      ->addError($this
      ->t('No valid custom link specified.'));
    $this
      ->redirect('xmlsitemap_custom.list');
  }
  else {
    $this->custom_link = $custom_link;
  }
  $form['type'] = [
    '#type' => 'value',
    '#value' => 'custom',
  ];
  $form['subtype'] = [
    '#type' => 'value',
    '#value' => '',
  ];
  $form['id'] = [
    '#type' => 'value',
    '#value' => $this->custom_link['id'],
  ];
  $form['loc'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Path to link'),
    '#field_prefix' => rtrim(Url::fromRoute('<front>', [], [
      'absolute' => TRUE,
    ])
      ->toString(), '/'),
    '#default_value' => $this->custom_link['loc'],
    '#description' => $this
      ->t('Use a relative path with a slash in front. For example, "/about".'),
    '#required' => TRUE,
    '#size' => 30,
  ];
  $form['priority'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Priority'),
    '#options' => xmlsitemap_get_priority_options(),
    '#default_value' => number_format($this->custom_link['priority'], 1),
    '#description' => $this
      ->t('The priority of this URL relative to other URLs on your site.'),
  ];
  $form['changefreq'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Change frequency'),
    '#options' => [
      0 => $this
        ->t('None'),
    ] + xmlsitemap_get_changefreq_options(),
    '#default_value' => $this->custom_link['changefreq'],
    '#description' => $this
      ->t('How frequently the page is likely to change. This value provides general information to search engines and may not correlate exactly to how often they crawl the page.'),
  ];
  $form['language'] = [
    '#type' => 'language_select',
    '#title' => $this
      ->t('Language'),
    '#languages' => LanguageInterface::STATE_ALL,
    '#default_value' => $this->custom_link['language'],
  ];
  $form['actions'] = [
    '#type' => 'actions',
  ];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Save'),
    '#weight' => 5,
    '#button_type' => 'primary',
  ];
  $form['actions']['cancel'] = [
    '#type' => 'link',
    '#title' => $this
      ->t('Cancel'),
    '#url' => Url::fromRoute('xmlsitemap_custom.list'),
    '#weight' => 10,
  ];
  return $form;
}