You are here

public function MetatagFirehose::massageFormValues in Metatag 8

Massages the form values into the format expected for field values.

Parameters

array $values: The submitted form values produced by the widget.

  • If the widget does not manage multiple values itself, the array holds the values generated by the multiple copies of the $element generated by the formElement() method, keyed by delta.
  • If the widget manages multiple values, the array holds the values of the form element generated by the formElement() method.

array $form: The form structure where field elements are attached to. This might be a full form structure, or a sub-element of a larger form.

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

Return value

array An array of field values, keyed by delta.

Overrides WidgetBase::massageFormValues

File

src/Plugin/Field/FieldWidget/MetatagFirehose.php, line 175

Class

MetatagFirehose
Advanced widget for metatag field.

Namespace

Drupal\metatag\Plugin\Field\FieldWidget

Code

public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {

  // Flatten the values array to remove the groups and then serialize all the
  // meta tags into one value for storage.
  $tag_manager = $this->metatagPluginManager;
  foreach ($values as &$value) {
    $flattened_value = [];
    foreach ($value as $group) {

      // Exclude the "original delta" value.
      if (is_array($group)) {
        foreach ($group as $tag_id => $tag_value) {
          $tag = $tag_manager
            ->createInstance($tag_id);
          $tag
            ->setValue($tag_value);
          if (!empty($tag
            ->value())) {
            $flattened_value[$tag_id] = $tag
              ->value();
          }
        }
      }
    }
    $value = serialize($flattened_value);
  }
  return $values;
}