/**
  * Submission callback for the first step.
  */
 public function stepOneSubmit($form, FormStateInterface $form_state)
 {
     // Increment the internal step counter.
     $this->step++;
     $cached_values = $form_state->getTemporaryValue('wizard');
     $parameters = $this->getNextParameters($cached_values);
     // This is validated in EmbridgeSearchForm::validateForm().
     $result_chosen = $form_state->getUserInput()['result_chosen'];
     $asset = EmbridgeAssetEntity::load($result_chosen);
     $image_element = ['src' => \Drupal::getContainer()->get('embridge.asset_helper')->getAssetConversionUrl($asset, 'emshare', 'thumb'), 'data-entity-uuid' => $asset->uuid(), 'data-entity-type' => 'embridge_asset_entity', 'alt' => '', 'width' => '', 'height' => ''];
     $parameters['image_element'] = $image_element;
     $form_state->setTemporaryValue('wizard', $parameters);
     $form_state->setRebuild();
 }
 /**
  * Form element validation callback for upload element on file widget.
  *
  * Checks if user has uploaded more files than allowed.
  *
  * This validator is used only when cardinality not set to 1 or unlimited.
  *
  * @param array $element
  *   The element.
  * @param FormStateInterface $form_state
  *   The form state.
  * @param array $form
  *   The form.
  */
 public static function validateMultipleCount($element, FormStateInterface $form_state, $form)
 {
     $parents = $element['#parents'];
     $values = NestedArray::getValue($form_state->getValues(), $parents);
     array_pop($parents);
     $current = count(Element::children(NestedArray::getValue($form, $parents))) - 1;
     $field_storage_definitions = \Drupal::entityManager()->getFieldStorageDefinitions($element['#entity_type']);
     $field_storage = $field_storage_definitions[$element['#field_name']];
     $uploaded = count($values['fids']);
     $count = $uploaded + $current;
     if ($count > $field_storage->getCardinality()) {
         $keep = $uploaded - $count + $field_storage->getCardinality();
         $removed_files = array_slice($values['fids'], $keep);
         $removed_names = array();
         foreach ($removed_files as $id) {
             /** @var \Drupal\embridge\EmbridgeAssetEntityInterface $asset */
             $asset = EmbridgeAssetEntity::load($id);
             $removed_names[] = $asset->getFilename();
         }
         $args = array('%field' => $field_storage->getName(), '@max' => $field_storage->getCardinality(), '@count' => $uploaded, '%list' => implode(', ', $removed_names));
         $message = t('Field %field can only hold @max values but there were @count uploaded. The following files have been omitted as a result: %list.', $args);
         drupal_set_message($message, 'warning');
         $values['fids'] = array_slice($values['fids'], 0, $keep);
         NestedArray::setValue($form_state->getValues(), $element['#parents'], $values);
     }
 }
示例#3
0
 /**
  * {@inheritdoc}
  */
 public static function valueCallback(&$element, $input, FormStateInterface $form_state)
 {
     // Find the current value of this field.
     $fids = !empty($input['fids']) ? explode(' ', $input['fids']) : [];
     foreach ($fids as $key => $fid) {
         $fids[$key] = (int) $fid;
     }
     // Process any input and save new uploads.
     if ($input !== FALSE) {
         $input['fids'] = $fids;
         $return = $input;
         // Uploads take priority over all other values.
         if ($files = self::saveFileUpload($element, $form_state)) {
             if ($element['#multiple']) {
                 $fids = array_merge($fids, array_keys($files));
             } else {
                 $fids = array_keys($files);
             }
         } else {
             // Check for #filefield_value_callback values.
             // Because FAPI does not allow multiple #value_callback values like it
             // does for #element_validate and #process, this fills the missing
             // functionality to allow File fields to be extended through FAPI.
             if (isset($element['#file_value_callbacks'])) {
                 foreach ($element['#file_value_callbacks'] as $callback) {
                     $callback($element, $input, $form_state);
                 }
             }
             // Load files if the FIDs have changed to confirm they exist.
             if (!empty($input['fids'])) {
                 $fids = [];
                 foreach ($input['fids'] as $fid) {
                     if ($file = EmbridgeAssetEntity::load($fid)) {
                         $fids[] = $file->id();
                     }
                 }
             }
         }
     } else {
         if ($element['#extended']) {
             $default_fids = isset($element['#default_value']['fids']) ? $element['#default_value']['fids'] : [];
             $return = isset($element['#default_value']) ? $element['#default_value'] : ['fids' => []];
         } else {
             $default_fids = isset($element['#default_value']) ? $element['#default_value'] : [];
             $return = ['fids' => []];
         }
         // Confirm that the file exists when used as a default value.
         if (!empty($default_fids)) {
             $fids = [];
             foreach ($default_fids as $fid) {
                 if ($file = EmbridgeAssetEntity::load($fid)) {
                     $fids[] = $file->id();
                 }
             }
         }
     }
     $return['fids'] = $fids;
     return $return;
 }