You are here

public function StaticPageSettingsForm::buildForm in Static Page 8

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 ConfigFormBase::buildForm

File

src/Form/StaticPageSettingsForm.php, line 59

Class

StaticPageSettingsForm
Configure search settings for this site.

Namespace

Drupal\static_page\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $config = $this
    ->config('static_page.fields');
  $types = NodeType::loadMultiple();
  $fields_config = $config
    ->get('fields');
  $form['help'] = [
    '#markup' => $this
      ->t('Select which field, if any, to use for static page content.'),
  ];
  $form['fields'] = [
    '#tree' => TRUE,
  ];
  $valid_field_types = [
    'string_long',
    'text_long',
    'text_with_summary',
  ];
  foreach ($types as $key => $node_type) {
    $field_options = [
      '' => $this
        ->t('-- None --'),
    ];
    $fields = $this->entityFieldManager
      ->getFieldDefinitions('node', $key);
    foreach ($fields as $machine_name => $field) {
      if ($field
        ->getName() != 'revision_log' && in_array($field
        ->getType(), $valid_field_types)) {
        $field_options[$machine_name] = $field
          ->getLabel() . ' (' . $field
          ->getName() . ')';
      }
    }
    $form['fields'][$key] = [
      '#title' => $node_type
        ->label(),
      '#type' => 'select',
      '#options' => $field_options,
      '#default_value' => !empty($fields_config[$key]) ? $fields_config[$key] : '',
    ];
  }
  return parent::buildForm($form, $form_state);
}