You are here

public function LanguageConfigEntityFormBase::buildForm in Flags 8

Overrides Drupal\Core\Entity\EntityFormController::form().

Builds the entity add/edit form.

Parameters

array $form: An associative array containing the structure of the form.

FormStateInterface $form_state: An instance of FormStateInterface containing the current state of the form.

Return value

array An associative array containing the FlagMapping add/edit form.

Overrides EntityForm::buildForm

1 call to LanguageConfigEntityFormBase::buildForm()
LanguageMappingEditForm::buildForm in flags_ui/src/Form/LanguageMappingEditForm.php
@inheritDoc
1 method overrides LanguageConfigEntityFormBase::buildForm()
LanguageMappingEditForm::buildForm in flags_ui/src/Form/LanguageMappingEditForm.php
@inheritDoc

File

flags_ui/src/Form/LanguageConfigEntityFormBase.php, line 77

Class

LanguageConfigEntityFormBase
Class ConfigEntityFormBase.

Namespace

Drupal\flags_ui\Form

Code

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

  // Get anything we need from the base class.
  $form = parent::buildForm($form, $form_state);

  // Drupal provides the entity to us as a class variable. If this is an
  // existing entity, it will be populated with existing values as class
  // variables. If this is a new entity, it will be a new object with the
  // class of our entity. Drupal knows which class to call from the
  // annotation on our FlagMapping class.

  /** @var FlagMapping $mapping */
  $mapping = $this->entity;
  $languages = \Drupal::service('flags.language_helper')
    ->getAllDefinedLanguages();
  if (\Drupal::moduleHandler()
    ->moduleExists('select_icons')) {
    $flagAttributes = $this
      ->getAttributes(array_keys($this->flags));
    $languageAttributes = $this
      ->getAttributes(array_keys($languages));
    $form['source'] = [
      '#type' => 'select_icons',
      '#options_attributes' => $languageAttributes,
    ];
    $form['flag'] = [
      '#type' => 'select_icons',
      '#options_attributes' => $flagAttributes,
      '#attached' => array(
        'library' => array(
          'flags/flags',
        ),
      ),
    ];
  }
  else {
    $form['source']['#type'] = 'select';
    $form['flag']['#type'] = 'select';
  }
  $form['source'] += [
    '#title' => $this
      ->t('Language'),
    '#default_value' => $mapping
      ->getSource(),
    '#description' => $this
      ->t('Select a source language.'),
    '#options' => $this->languageManager
      ->getAllDefinedLanguages(),
    '#required' => TRUE,
  ];
  $form['flag'] += [
    '#title' => $this
      ->t('Flag'),
    '#options' => $this->flags,
    '#empty_value' => '',
    '#default_value' => $mapping
      ->getFlag(),
    '#description' => $this
      ->t('Select a target territory flag.'),
    '#required' => TRUE,
  ];

  // Return the form.
  return $form;
}