You are here

public function Taxonomy::massageFormValues in Workbench Access 8

Massage form values as appropriate during entity submit.

This method is invoked by submitEntity() to save items passed by the disallowedOptions() method.

Parameters

\Drupal\Core\Entity\ContentEntityInterface $entity: Entity being edited.

\Drupal\Core\Form\FormStateInterface $form_state: Form state.

array $hidden_values: Hidden values passed by the form, generally from disallowedOptions().

Overrides AccessControlHierarchyBase::massageFormValues

File

src/Plugin/AccessControlHierarchy/Taxonomy.php, line 320

Class

Taxonomy
Defines a hierarchy based on a Vocabulary.

Namespace

Drupal\workbench_access\Plugin\AccessControlHierarchy

Code

public function massageFormValues(ContentEntityInterface $entity, FormStateInterface $form_state, array $hidden_values) {
  foreach (array_column($this
    ->getApplicableFields($entity
    ->getEntityTypeId(), $entity
    ->bundle()), 'field') as $field_name) {
    $values = $form_state
      ->getValue($field_name);

    // The $hidden_values are deeply nested.
    foreach ($hidden_values as $key => $value) {
      if ($key === $field_name) {
        foreach ($value as $element) {
          foreach ($element as $item) {

            // Ensure that we do not save duplicate values. Note that this
            // cannot be a strict in_array() check thanks to form handling.
            if (empty($values[0]) || !in_array($item, array_values($values[0]))) {
              $values[]['target_id'] = $item;
            }
          }
        }
      }
    }
    $form_state
      ->setValue($field_name, $values);
  }
}