You are here

public function XmlSitemapCustomAddForm::buildForm in XML sitemap 8

Same name and namespace in other branches
  1. 2.x xmlsitemap_custom/src/Form/XmlSitemapCustomAddForm.php \Drupal\xmlsitemap_custom\Form\XmlSitemapCustomAddForm::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/XmlSitemapCustomAddForm.php, line 101

Class

XmlSitemapCustomAddForm
Provides a form for adding a custom link.

Namespace

Drupal\xmlsitemap_custom\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {

  // Take into account that databases use wildly different names for their
  // data types.
  $db_type = $this->connection
    ->databaseType();
  switch ($db_type) {
    case 'mysql':
      $type = 'UNSIGNED';
      break;
    case 'pgsql':
      $type = 'BIGINT';
      break;
    case 'sqlite':
      $type = 'INTEGER';
      break;
    default:
      $type = 'INT';
      break;
  }
  $query = $this->connection
    ->select('xmlsitemap', 'x');
  $query
    ->addExpression("MAX(CAST(id AS {$type}))");
  $query
    ->condition('type', 'custom');
  $id = (int) $query
    ->execute()
    ->fetchField();
  $link = [
    'id' => $id + 1,
    'loc' => '',
    'priority' => XMLSITEMAP_PRIORITY_DEFAULT,
    'lastmod' => 0,
    'changefreq' => 0,
    'changecount' => 0,
    'language' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
  ];
  $form['type'] = [
    '#type' => 'value',
    '#value' => 'custom',
  ];
  $form['subtype'] = [
    '#type' => 'value',
    '#value' => '',
  ];
  $form['id'] = [
    '#type' => 'value',
    '#value' => $link['id'],
  ];
  $form['loc'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Path to link'),
    '#field_prefix' => rtrim(Url::fromRoute('<front>', [], [
      'absolute' => TRUE,
    ])
      ->toString(), '/'),
    '#default_value' => $link['loc'] ? $this->aliasManager
      ->getPathByAlias($link['loc'], $link['language']) : '',
    '#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($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' => $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' => $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;
}