You are here

public function ReadingTimeConfigForm::buildForm in Node read time 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/ReadingTimeConfigForm.php, line 53

Class

ReadingTimeConfigForm
Defines a form for enabling reading time field for specific content types.

Namespace

Drupal\node_read_time\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $form = parent::buildForm($form, $form_state);
  $config = $this
    ->config('node_read_time.settings');
  $contentTypes = $this->entityTypeManager
    ->getStorage('node_type')
    ->loadMultiple();
  $words_per_minute = !empty($config
    ->get('reading_time')['words_per_minute']) ? $config
    ->get('reading_time')['words_per_minute'] : NULL;
  $unit = !empty($config
    ->get('reading_time')['unit_of_time']) ? $config
    ->get('reading_time')['unit_of_time'] : 'minute';
  $only_minute = $this
    ->t('@minute', [
    '@minute' => 'Only Minute',
  ]);
  $minute_second = $this
    ->t('@second', [
    '@second' => 'Minute and Second',
  ]);
  $default = $this
    ->t('@default', [
    '@default' => 'Default (only minute) without text',
  ]);
  $form['container'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Content types to apply reading time field to.'),
    '#tree' => TRUE,
  ];
  $form['words_per_minute'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('Words per minute.'),
    '#default_value' => $words_per_minute,
  ];
  $form['unit_of_time'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Unit of time'),
    '#options' => [
      'default' => $default,
      'minute' => $only_minute,
      'second' => $minute_second,
    ],
    '#default_value' => $unit,
  ];
  foreach ($contentTypes as $type => $obj) {
    $name = $obj
      ->get('name');
    $is_activated = !empty($config
      ->get('reading_time')['container'][$type]['is_activated']) ? $config
      ->get('reading_time')['container'][$type]['is_activated'] : NULL;
    $form['container'][$type]['is_activated'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Activate for @name', [
        '@name' => $name,
      ]),
      '#default_value' => $is_activated,
      '#tree' => TRUE,
    ];
  }
  return $form;
}