You are here

public function RecentlyReadConfigForm::buildForm in Recently Read 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/RecentlyReadConfigForm.php, line 88

Class

RecentlyReadConfigForm
Class RecentlyReadConfig.

Namespace

Drupal\recently_read\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $config = $this->configFactory
    ->get('recently_read.configuration');
  $recentlyReadEntityTypes = [];

  // Get all content entity types.
  $entityTypes = $this->entityTypeManager
    ->getDefinitions();
  foreach ($entityTypes as $entityType) {
    if ($entityType
      ->getGroup() === 'content') {
      $recentlyReadEntityTypes[$entityType
        ->id()] = $entityType
        ->getLabel();
    }
  }
  $enabledEntityTypes = array_keys(RecentlyReadType::loadMultiple());
  $form['entity_types'] = [
    '#type' => 'checkboxes',
    '#title' => $this
      ->t('Entity types'),
    '#options' => $recentlyReadEntityTypes,
    '#default_value' => $enabledEntityTypes,
  ];
  $form['delete_config'] = [
    '#type' => 'radios',
    '#title' => 'Records delete options',
    '#options' => [
      'time' => $this
        ->t('Time based'),
      'count' => $this
        ->t('Count based'),
      'never' => $this
        ->t('Never'),
    ],
    '#default_value' => $config
      ->get('delete_config'),
  ];
  $delete_time_options = [
    '-1 hours' => '1 hours',
    '-1 day' => '1 day',
    '-1 week' => '1 Week',
    '-1 month' => '1 Month',
    '-1 year' => '1 Year',
  ];
  $form['delete_time'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Delete records older then'),
    '#description' => $this
      ->t('When cron is executed, delete records that are older then selected value.'),
    '#options' => $delete_time_options,
    '#default_value' => $config
      ->get('delete_time'),
    '#states' => [
      'visible' => [
        ':input[name="delete_config"]' => [
          'value' => 'time',
        ],
      ],
    ],
  ];
  $form['count'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('Max records'),
    '#description' => $this
      ->t('Allowed number of records per user or session (if user is anonymous). Older records will be removed.'),
    '#default_value' => $config
      ->get('count'),
    '#states' => [
      'visible' => [
        ':input[name="delete_config"]' => [
          'value' => 'count',
        ],
      ],
      'required' => [
        ':input[name="delete_config"]' => [
          'value' => 'count',
        ],
      ],
    ],
  ];
  return parent::buildForm($form, $form_state);
}