You are here

AddForm.php in Custom Meta 2.0.x

File

src/Form/AddForm.php
View source
<?php

namespace Drupal\custom_meta\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;

/**
 * Provides the custom meta tag add form.
 */
class AddForm extends FormBase {

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'custom_meta_admin_add';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state, $id = NULL) {
    $meta = [];
    if ($id) {
      $meta = $this
        ->config('custom_meta.settings')
        ->get('tag')[$id];
      $form['id'] = array(
        '#type' => 'hidden',
        '#value' => $id,
      );
    }
    $form['attribute'] = array(
      '#type' => 'select',
      '#title' => $this
        ->t('Meta attribute'),
      '#options' => [
        'name' => 'Name',
        'property' => 'Property',
        'http-equiv' => 'Http Equiv',
      ],
      '#description' => t('Specify custom meta tag attribute.'),
      '#required' => TRUE,
      '#default_value' => $meta['attribute'] ?? '',
    );
    $form['name'] = array(
      '#type' => 'textfield',
      '#title' => $this
        ->t('Meta name'),
      '#maxlength' => 255,
      '#description' => t('Specify custom meta tag name.'),
      '#required' => TRUE,
      '#default_value' => $meta['name'] ?? '',
    );
    $form['label'] = array(
      '#type' => 'textfield',
      '#title' => $this
        ->t('Meta label'),
      '#maxlength' => 255,
      '#description' => t('Specify custom meta tag label.'),
      '#required' => TRUE,
      '#default_value' => $meta['label'] ?? '',
    );
    $form['description'] = array(
      '#type' => 'textfield',
      '#title' => $this
        ->t('Meta description'),
      '#maxlength' => 255,
      '#description' => t('Specify custom meta tag description.'),
      '#required' => TRUE,
      '#default_value' => $meta['description'] ?? '',
    );
    $form['actions'] = array(
      '#type' => 'actions',
    );
    $form['actions']['submit'] = array(
      '#type' => 'submit',
      '#value' => $this
        ->t('Save'),
    );
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    if (!$form_state
      ->getValue('id') && array_key_exists($form_state
      ->getValue('name'), $this
      ->config('custom_meta.settings')
      ->get('tag'))) {
      $form_state
        ->setErrorByName('name', t('The custom meta tag %tag already exists.', [
        '%tag' => $form_state
          ->getValue('name'),
      ]));
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $values = $this
      ->config('custom_meta.settings')
      ->get('tag');
    $values[$form_state
      ->getValue('name')] = [
      'attribute' => $form_state
        ->getValue('attribute'),
      'name' => $form_state
        ->getValue('name'),
      'label' => $form_state
        ->getValue('label'),
      'description' => $form_state
        ->getValue('description'),
    ];
    $this
      ->configFactory()
      ->getEditable('custom_meta.settings')
      ->set('tag', $values)
      ->save();
    \Drupal::messenger()
      ->addStatus('Meta tag has been saved.');
    $form_state
      ->setRedirect('custom_meta.admin_overview');
  }

}

Classes

Namesort descending Description
AddForm Provides the custom meta tag add form.