You are here

function smart_date_recur_form_field_config_edit_form_alter in Smart Date 3.0.x

Same name and namespace in other branches
  1. 8.2 modules/smart_date_recur/smart_date_recur.module \smart_date_recur_form_field_config_edit_form_alter()
  2. 3.x modules/smart_date_recur/smart_date_recur.module \smart_date_recur_form_field_config_edit_form_alter()
  3. 3.1.x modules/smart_date_recur/smart_date_recur.module \smart_date_recur_form_field_config_edit_form_alter()
  4. 3.2.x modules/smart_date_recur/smart_date_recur.module \smart_date_recur_form_field_config_edit_form_alter()
  5. 3.3.x modules/smart_date_recur/smart_date_recur.module \smart_date_recur_form_field_config_edit_form_alter()
  6. 3.4.x modules/smart_date_recur/smart_date_recur.module \smart_date_recur_form_field_config_edit_form_alter()

Add configuration elements for Smart Date fields.

File

modules/smart_date_recur/smart_date_recur.module, line 411
Field hooks for a field that stores a start and end date as timestamps.

Code

function smart_date_recur_form_field_config_edit_form_alter(&$form, FormStateInterface $form_state) {

  // Only try to add our option to Smart Date fields.
  $field = $form_state
    ->getFormObject()
    ->getEntity();
  if ($field
    ->getType() != 'smartdate') {
    return;
  }

  // Only provide the recurring option if unlimited values are allowed.
  $cardinality = $field
    ->getFieldStorageDefinition()
    ->getCardinality();
  if ($cardinality != -1) {
    $messenger = \Drupal::messenger();
    $messenger
      ->addMessage(t('Recurring values can only be used on Smart Date fields that allow unlimited values.'), 'warning');
    return;
  }
  $entity = $form_state
    ->getFormObject()
    ->getEntity();
  $form['third_party_settings']['smart_date_recur'] = [
    '#type' => 'details',
    '#title' => t('Recurring Dates'),
    '#open' => TRUE,
  ];
  $form['third_party_settings']['smart_date_recur']['allow_recurring'] = [
    '#type' => 'checkbox',
    '#title' => t('Allow recurring date values'),
    '#default_value' => $entity
      ->getThirdPartySetting('smart_date_recur', 'allow_recurring'),
  ];
  $months = $entity
    ->getThirdPartySetting('smart_date_recur', 'month_limit');
  $form['third_party_settings']['smart_date_recur']['month_limit'] = [
    '#type' => 'number',
    '#title' => t('Months to Extend'),
    '#description' => t('For recurring dates without a specified end, how many months out should instances be generated? If left empty or zero, a default value of 12 will be used.'),
    '#states' => [
      // Show this textarea only if the 'repeat' select has a value.
      'visible' => [
        'input[name="third_party_settings[smart_date_recur][allow_recurring]"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
    '#default_value' => $months ? $months : 12,
  ];
}