You are here

public function DefaultProcessor::buildConfigurationForm in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/aggregator/src/Plugin/aggregator/processor/DefaultProcessor.php \Drupal\aggregator\Plugin\aggregator\processor\DefaultProcessor::buildConfigurationForm()

Form constructor.

Plugin forms are embedded in other forms. In order to know where the plugin form is located in the parent form, #parents and #array_parents must be known, but these are not available during the initial build phase. In order to have these properties available when building the plugin form's elements, let this method return a form element that has a #process callback and build the rest of the form in the callback. By the time the callback is executed, the element's #parents and #array_parents properties will have been set by the form API. For more documentation on #parents and #array_parents, see \Drupal\Core\Render\Element\FormElement.

Parameters

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

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form. Calling code should pass on a subform state created through \Drupal\Core\Form\SubformState::createForSubform().

Return value

array The form structure.

Overrides PluginFormInterface::buildConfigurationForm

File

core/modules/aggregator/src/Plugin/aggregator/processor/DefaultProcessor.php, line 117

Class

DefaultProcessor
Defines a default processor implementation.

Namespace

Drupal\aggregator\Plugin\aggregator\processor

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  $config = $this
    ->config('aggregator.settings');
  $processors = $config
    ->get('processors');
  $info = $this
    ->getPluginDefinition();
  $counts = [
    3,
    5,
    10,
    15,
    20,
    25,
  ];
  $items = array_map(function ($count) {
    return $this
      ->formatPlural($count, '1 item', '@count items');
  }, array_combine($counts, $counts));
  $intervals = [
    3600,
    10800,
    21600,
    32400,
    43200,
    86400,
    172800,
    259200,
    604800,
    1209600,
    2419200,
    4838400,
    9676800,
  ];
  $period = array_map([
    $this->dateFormatter,
    'formatInterval',
  ], array_combine($intervals, $intervals));
  $period[FeedStorageInterface::CLEAR_NEVER] = t('Never');
  $form['processors'][$info['id']] = [];

  // Only wrap into details if there is a basic configuration.
  if (isset($form['basic_conf'])) {
    $form['processors'][$info['id']] = [
      '#type' => 'details',
      '#title' => t('Default processor settings'),
      '#description' => $info['description'],
      '#open' => in_array($info['id'], $processors),
    ];
  }
  $form['processors'][$info['id']]['aggregator_summary_items'] = [
    '#type' => 'select',
    '#title' => t('Number of items shown in listing pages'),
    '#default_value' => $config
      ->get('source.list_max'),
    '#empty_value' => 0,
    '#options' => $items,
  ];
  $form['processors'][$info['id']]['aggregator_clear'] = [
    '#type' => 'select',
    '#title' => t('Discard items older than'),
    '#default_value' => $config
      ->get('items.expire'),
    '#options' => $period,
    '#description' => t('Requires a correctly configured <a href=":cron">cron maintenance task</a>.', [
      ':cron' => Url::fromRoute('system.status')
        ->toString(),
    ]),
  ];
  $lengths = [
    0,
    200,
    400,
    600,
    800,
    1000,
    1200,
    1400,
    1600,
    1800,
    2000,
  ];
  $options = array_map(function ($length) {
    return $length == 0 ? t('Unlimited') : $this
      ->formatPlural($length, '1 character', '@count characters');
  }, array_combine($lengths, $lengths));
  $form['processors'][$info['id']]['aggregator_teaser_length'] = [
    '#type' => 'select',
    '#title' => t('Length of trimmed description'),
    '#default_value' => $config
      ->get('items.teaser_length'),
    '#options' => $options,
    '#description' => t('The maximum number of characters used in the trimmed version of content.'),
  ];
  return $form;
}