You are here

public function SimplemetaEntityForm::buildForm in Simple Meta 8.2

Same name and namespace in other branches
  1. 8 src/Form/SimplemetaEntityForm.php \Drupal\simplemeta\Form\SimplemetaEntityForm::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 EntityForm::buildForm

File

src/Form/SimplemetaEntityForm.php, line 18

Class

SimplemetaEntityForm
Form controller for SimpleMeta edit forms.

Namespace

Drupal\simplemeta\Form

Code

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

  /* @var $entity \Drupal\simplemeta\Entity\SimplemetaEntity */
  $form = parent::buildForm($form, $form_state);
  $entity = $this->entity;

  // Prefill current path for better UX.
  if (empty($form['url']['widget'][0]['value']['#default_value'])) {
    $current_path = \Drupal::service('path.current')
      ->getPath();
    $form['url']['widget'][0]['value']['#default_value'] = $current_path;
  }
  $form['data'] = [
    '#tree' => TRUE,
  ];

  // Meta title.
  $form['data']['title'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Title'),
    '#maxlength' => 255,
    '#default_value' => $entity->data->title,
  ];

  // Meta description.
  $form['data']['description'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Description'),
    '#resizable' => FALSE,
    '#default_value' => $entity->data->description,
  ];
  $form['data']['keywords'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Keywords'),
    '#description' => $this
      ->t('Comma-separated list of keywords.'),
    '#maxlength' => 255,
    '#default_value' => $entity->data->keywords,
  ];
  return $form;
}