You are here

public function EasyMetaForm::buildForm in Easymeta 8

Implements \Drupal\Core\Form\FormInterface::buildForm().

Overrides FormInterface::buildForm

File

src/Form/EasyMetaForm.php, line 28

Class

EasyMetaForm
Build and process meta form.

Namespace

Drupal\easymeta\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $configuration = 'NULL') {
  $this
    ->init();
  $current_path = \Drupal::service('path.current')
    ->getPath();
  $current_language = \Drupal::languageManager()
    ->getCurrentLanguage()
    ->getId();
  $meta = new Meta($current_language, $current_path);
  $meta_values = $meta
    ->getValue();
  foreach ($this->metasType as $meta_type) {

    /* @var $meta_type MetaType */
    $value = NULL;
    if (isset($meta_values[$meta_type
      ->getName()])) {
      $value = $meta_values[$meta_type
        ->getName()]['value'] ? $meta_values[$meta_type
        ->getName()]['value'] : '';
    }
    $value = !empty($value) ? $value : $meta_type
      ->getDefaultValue();
    $form['meta'][$meta_type
      ->getName()] = [
      '#type' => $meta_type
        ->getFieldType(),
      '#title' => $meta_type
        ->getLabel(),
      '#required' => FALSE,
      '#default_value' => $value,
    ];
    if ($meta_type
      ->getFieldType() == "select") {
      $form['meta'][$meta_type
        ->getName()]['#options'] = $meta_type
        ->getOptions();
      $form['meta'][$meta_type
        ->getName()]['#empty_option'] = t("None");
    }
    if ($meta_type
      ->getFieldType() == "managed_file") {
      $form['meta'][$meta_type
        ->getName()]['#upload_location'] = 'public://';
    }
  }
  $form['meta_id'] = array(
    '#type' => 'hidden',
    '#value' => $meta
      ->getId() ? $meta
      ->getId() : '',
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );
  $form['markup'] = array(
    '#markup' => '<span class="easymeta-open">' . t("Meta") . '</span>',
  );
  return $form;
}