You are here

public function VideoEmbedWidget::extractFormValues in Video 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/Field/FieldWidget/VideoEmbedWidget.php \Drupal\video\Plugin\Field\FieldWidget\VideoEmbedWidget::extractFormValues()

Extracts field values from submitted form values.

Parameters

\Drupal\Core\Field\FieldItemListInterface $items: The field values. This parameter is altered by reference to receive the incoming form values.

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.

Overrides FileWidget::extractFormValues

File

src/Plugin/Field/FieldWidget/VideoEmbedWidget.php, line 359

Class

VideoEmbedWidget
Plugin implementation of the 'video_embed' widget.

Namespace

Drupal\video\Plugin\Field\FieldWidget

Code

public function extractFormValues(FieldItemListInterface $items, array $form, FormStateInterface $form_state) {
  $field_name = $this->fieldDefinition
    ->getName();

  // Extract the values from $form_state->getValues().
  $path = array_merge($form['#parents'], [
    $field_name,
  ]);
  $key_exists = NULL;
  $values = NestedArray::getValue($form_state
    ->getValues(), $path, $key_exists);
  if ($key_exists) {

    // Account for drag-and-drop reordering if needed.
    if (!$this
      ->handlesMultipleValues()) {

      // Remove the 'value' of the 'add more' button.
      unset($values['add_more']);

      // The original delta, before drag-and-drop reordering, is needed to
      // route errors to the correct form element.
      foreach ($values as $delta => &$value) {
        $value['_original_delta'] = $delta;
      }
      usort($values, function ($a, $b) {
        return SortArray::sortByKeyInt($a, $b, '_weight');
      });
    }

    // Let the widget massage the submitted values.
    foreach ($values as $delta => &$value) {
      if (!empty($value['value']) && empty($value['fids'])) {

        // ready to save the file
        $provider_manager = \Drupal::service('video.provider_manager');
        $allowed_providers = $this
          ->getSetting('allowed_providers');
        $enabled_providers = $provider_manager
          ->loadDefinitionsFromOptionList($allowed_providers);
        if ($provider_matches = $provider_manager
          ->loadApplicableDefinitionMatches($enabled_providers, $value['value'])) {
          $definition = $provider_matches['definition'];
          $matches = $provider_matches['matches'];
          $uri = $definition['stream_wrapper'] . '://' . $matches['id'];
          $storage = \Drupal::entityManager()
            ->getStorage('file');
          $results = $storage
            ->getQuery()
            ->condition('uri', $uri)
            ->execute();
          if (!(count($results) > 0)) {
            $user = \Drupal::currentUser();
            $file = File::Create([
              'uri' => $uri,
              'filemime' => $definition['mimetype'],
              'filesize' => 1,
              'uid' => $user
                ->id(),
            ]);
            $file
              ->save();
            unset($values[$delta]);
            $values[] = [
              'fids' => [
                $file
                  ->id(),
              ],
              'data' => serialize($matches),
            ];
          }
          else {
            unset($values[$delta]);
            $values[] = [
              'fids' => [
                reset($results),
              ],
              'data' => serialize($matches),
            ];
          }
        }
      }
      if (!isset($value['fids'])) {

        // If fids is still not set, remove this value, otherwise massageFormValues() will fail.
        unset($values[$delta]);
      }
    }
    $values = $this
      ->massageFormValues($values, $form, $form_state);

    // Assign the values and remove the empty ones.
    $items
      ->setValue($values);
    $items
      ->filterEmptyItems();

    // Put delta mapping in $form_state, so that flagErrors() can use it.
    $field_state = static::getWidgetState($form['#parents'], $field_name, $form_state);
    foreach ($items as $delta => $item) {
      $field_state['original_deltas'][$delta] = isset($item->_original_delta) ? $item->_original_delta : $delta;
      unset($item->_original_delta, $item->_weight);
    }
    static::setWidgetState($form['#parents'], $field_name, $form_state, $field_state);
  }
}