You are here

public static function DateRecurItem::partsAfterBuild in Recurring Dates Field 3.0.x

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

After build used to format of submitted values.

FormBuilder has finished processing the input of children, now re-arrange the values.

Parameters

array $element: An associative array containing the structure of the element.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The new structure of the element.

File

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

Class

DateRecurItem
Plugin implementation of the 'date_recur' field type.

Namespace

Drupal\date_recur\Plugin\Field\FieldType

Code

public static function partsAfterBuild(array $element, FormStateInterface $form_state) : array {

  // Original parts container.
  $values = NestedArray::getValue($form_state
    ->getValues(), $element['#parents']);

  // Remove the original parts values so they dont get saved in same structure
  // as the form.
  NestedArray::unsetValue($form_state
    ->getValues(), $element['#parents']);
  $parts = [];
  $parts['all'] = !empty($values['all']);
  $parts['frequencies'] = [];
  foreach ($values['table'] as $frequency => $row) {
    $enabledParts = array_keys(array_filter($row['parts']));
    if ($row['setting'] === static::FREQUENCY_SETTINGS_PARTS_ALL) {
      $enabledParts[] = static::PART_SUPPORTS_ALL;
    }
    elseif ($row['setting'] === static::FREQUENCY_SETTINGS_DISABLED) {
      $enabledParts = [];
    }

    // Sort in order so config always looks consistent.
    sort($enabledParts);
    $parts['frequencies'][$frequency] = $enabledParts;
  }

  // Set the new value.
  $form_state
    ->setValue($element['#parents'], $parts);
  return $element;
}