You are here

public function DateRecurItem::fieldSettingsForm in Recurring Dates Field 3.1.x

Same name and namespace in other branches
  1. 8.2 src/Plugin/Field/FieldType/DateRecurItem.php \Drupal\date_recur\Plugin\Field\FieldType\DateRecurItem::fieldSettingsForm()
  2. 8 src/Plugin/Field/FieldType/DateRecurItem.php \Drupal\date_recur\Plugin\Field\FieldType\DateRecurItem::fieldSettingsForm()
  3. 3.x src/Plugin/Field/FieldType/DateRecurItem.php \Drupal\date_recur\Plugin\Field\FieldType\DateRecurItem::fieldSettingsForm()
  4. 3.0.x src/Plugin/Field/FieldType/DateRecurItem.php \Drupal\date_recur\Plugin\Field\FieldType\DateRecurItem::fieldSettingsForm()

Returns a form for the field-level settings.

Invoked from \Drupal\field_ui\Form\FieldConfigEditForm to allow administrators to configure field-level settings.

Parameters

array $form: The form where the settings form is being included in.

\Drupal\Core\Form\FormStateInterface $form_state: The form state of the (entire) configuration form.

Return value

array The form definition for the field settings.

Overrides FieldItemBase::fieldSettingsForm

File

src/Plugin/Field/FieldType/DateRecurItem.php, line 187

Class

DateRecurItem
Plugin implementation of the 'date_recur' field type.

Namespace

Drupal\date_recur\Plugin\Field\FieldType

Code

public function fieldSettingsForm(array $form, FormStateInterface $form_state) : array {

  // Its not possible to locate the parent from FieldConfigEditForm.
  $elementParts = [
    'settings',
  ];
  $element = parent::fieldSettingsForm($form, $form_state);

  // @todo Needs UI tests.
  $options = [];
  foreach (range(1, 5) as $i) {
    $options['P' . $i . 'Y'] = $this
      ->formatPlural($i, '@year year', '@year years', [
      '@year' => $i,
    ]);
  }

  // @todo allow custom values.
  $element['precreate'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Precreate occurrences'),
    '#description' => $this
      ->t('For infinitely repeating dates, precreate occurrences for this amount of time in the views cache table.'),
    '#options' => $options,
    '#default_value' => $this
      ->getSetting('precreate'),
  ];
  $element['parts'] = [
    '#type' => 'container',
  ];
  $element['parts']['#after_build'][] = [
    get_class($this),
    'partsAfterBuild',
  ];
  $allPartsSettings = $this
    ->getSetting('parts');
  $element['parts']['all'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Allow all frequency and parts'),
    '#default_value' => $allPartsSettings['all'] ?? TRUE,
  ];
  $parents = array_merge($elementParts, [
    'parts',
    'all',
  ]);

  // The form 'name' attribute of the 'all' parts checkbox above.
  $allPartsCheckboxName = $parents[0] . '[' . implode('][', array_slice($parents, 1)) . ']';
  $frequencyLabels = DateRecurRruleMap::frequencyLabels();
  $partLabels = DateRecurRruleMap::partLabels();
  $partsCheckboxes = [];
  foreach (DateRecurRruleMap::PARTS as $part) {
    $partsCheckboxes[$part] = [
      '#type' => 'checkbox',
      '#title' => $partLabels[$part],
    ];
  }
  $settingsOptions = [
    static::FREQUENCY_SETTINGS_DISABLED => $this
      ->t('Disabled'),
    static::FREQUENCY_SETTINGS_PARTS_ALL => $this
      ->t('All parts'),
    static::FREQUENCY_SETTINGS_PARTS_PARTIAL => $this
      ->t('Specify parts'),
  ];

  // Table is a container so visibility states can be added.
  $element['parts']['table'] = [
    '#theme' => 'date_recur_settings_frequency_table',
    '#type' => 'container',
    '#states' => [
      'visible' => [
        ':input[name="' . $allPartsCheckboxName . '"]' => [
          'checked' => FALSE,
        ],
      ],
    ],
  ];
  foreach (DateRecurRruleMap::FREQUENCIES as $frequency) {
    $row = [];
    $row['frequency']['#markup'] = $frequencyLabels[$frequency];
    $parents = array_merge($elementParts, [
      'parts',
      'table',
      $frequency,
      'setting',
    ]);

    // Constructs a name that looks like
    // settings[parts][table][MINUTELY][setting].
    $settingsCheckboxName = $parents[0] . '[' . implode('][', array_slice($parents, 1)) . ']';
    $enabledParts = $allPartsSettings['frequencies'][$frequency] ?? [];
    $defaultSetting = NULL;
    if (count($enabledParts) === 0) {
      $defaultSetting = static::FREQUENCY_SETTINGS_DISABLED;
    }
    elseif (in_array(static::PART_SUPPORTS_ALL, $enabledParts)) {
      $defaultSetting = static::FREQUENCY_SETTINGS_PARTS_ALL;
    }
    elseif (count($enabledParts) > 0) {
      $defaultSetting = static::FREQUENCY_SETTINGS_PARTS_PARTIAL;
    }
    $row['setting'] = [
      '#type' => 'radios',
      '#options' => $settingsOptions,
      '#required' => TRUE,
      '#default_value' => $defaultSetting,
    ];
    $row['parts'] = $partsCheckboxes;
    foreach ($row['parts'] as $part => &$partsCheckbox) {
      $partsCheckbox['#states']['visible'][] = [
        ':input[name="' . $settingsCheckboxName . '"]' => [
          'value' => static::FREQUENCY_SETTINGS_PARTS_PARTIAL,
        ],
      ];
      $partsCheckbox['#default_value'] = in_array($part, $enabledParts, TRUE);
    }
    $element['parts']['table'][$frequency] = $row;
  }
  $list = [];
  $partLabels = DateRecurRruleMap::partLabels();
  foreach (DateRecurRruleMap::partDescriptions() as $part => $partDescription) {
    $list[] = $this
      ->t('<strong>@label:</strong> @description', [
      '@label' => $partLabels[$part],
      '@description' => $partDescription,
    ]);
  }
  $element['parts']['help']['#markup'] = '<ul><li>' . implode('</li><li>', $list) . '</li></ul></ul>';
  return $element;
}