You are here

public function FileBrowserWidget::massageFormValues in Entity Browser 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/Field/FieldWidget/FileBrowserWidget.php \Drupal\entity_browser\Plugin\Field\FieldWidget\FileBrowserWidget::massageFormValues()

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 EntityReferenceBrowserWidget::massageFormValues

File

src/Plugin/Field/FieldWidget/FileBrowserWidget.php, line 436

Class

FileBrowserWidget
Entity browser file widget.

Namespace

Drupal\entity_browser\Plugin\Field\FieldWidget

Code

public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {
  $ids = empty($values['target_id']) ? [] : explode(' ', trim($values['target_id']));
  $return = [];
  foreach ($ids as $id) {
    $id = explode(':', $id)[1];
    if (is_array($values['current']) && isset($values['current'][$id])) {
      $item_values = [
        'target_id' => $id,
        '_weight' => $values['current'][$id]['_weight'],
      ];
      if ($this->fieldDefinition
        ->getType() == 'file') {
        if (isset($values['current'][$id]['meta']['description'])) {
          $item_values['description'] = $values['current'][$id]['meta']['description'];
        }
        if ($this->fieldDefinition
          ->getSetting('display_field') && isset($values['current'][$id]['meta']['display_field'])) {
          $item_values['display'] = $values['current'][$id]['meta']['display_field'];
        }
      }
      if ($this->fieldDefinition
        ->getType() == 'image') {
        if (isset($values['current'][$id]['meta']['alt'])) {
          $item_values['alt'] = $values['current'][$id]['meta']['alt'];
        }
        if (isset($values['current'][$id]['meta']['title'])) {
          $item_values['title'] = $values['current'][$id]['meta']['title'];
        }
      }
      $return[] = $item_values;
    }
  }

  // Return ourself as the structure doesn't match the default.
  usort($return, function ($a, $b) {
    return SortArray::sortByKeyInt($a, $b, '_weight');
  });
  return array_values($return);
}