Example #1
0
 /**
  * {@inheritdoc}
  */
 public static function process(array &$element, FormStateInterface $form_state, array &$complete_form)
 {
     $element['filefield_reference'] = array('#weight' => 100.5, '#theme' => 'filefield_sources_element', '#source_id' => 'reference', '#filefield_source' => TRUE, '#filefield_sources_hint_text' => FILEFIELD_SOURCE_REFERENCE_HINT_TEXT);
     $autocomplete_route_parameters = array('entity_type' => $element['#entity_type'], 'bundle_name' => $element['#bundle'], 'field_name' => $element['#field_name']);
     $element['filefield_reference']['autocomplete'] = array('#type' => 'textfield', '#autocomplete_route_name' => 'filefield_sources.autocomplete', '#autocomplete_route_parameters' => $autocomplete_route_parameters, '#description' => filefield_sources_element_validation_help($element['#upload_validators']));
     $ajax_settings = ['url' => Url::fromRoute('file.ajax_upload'), 'options' => ['query' => ['element_parents' => implode('/', $element['#array_parents']), 'form_build_id' => $complete_form['form_build_id']['#value']]], 'wrapper' => $element['#id'] . '-ajax-wrapper', 'effect' => 'fade'];
     $element['filefield_reference']['select'] = ['#name' => implode('_', $element['#parents']) . '_autocomplete_select', '#type' => 'submit', '#value' => t('Select'), '#validate' => [], '#submit' => ['filefield_sources_field_submit'], '#limit_validation_errors' => [$element['#parents']], '#ajax' => $ajax_settings];
     return $element;
 }
Example #2
0
 /**
  * {@inheritdoc}
  */
 public static function process(array &$element, FormStateInterface $form_state, array &$complete_form)
 {
     $element['filefield_clipboard'] = array('#weight' => 100.5, '#theme' => 'filefield_sources_element', '#source_id' => 'clipboard', '#filefield_source' => TRUE, '#filefield_sources_hint_text' => t('Enter filename then paste.'), '#description' => filefield_sources_element_validation_help($element['#upload_validators']));
     $element['filefield_clipboard']['capture'] = array('#type' => 'item', '#markup' => '<div class="filefield-source-clipboard-capture" contenteditable="true"><span class="hint">example_filename.png</span></div> <span class="hint">' . t('ctrl + v') . '</span>', '#description' => t('Enter a file name and paste an image from the clipboard. This feature only works in <a href="http://drupal.org/node/1775902">limited browsers</a>.'));
     $element['filefield_clipboard']['filename'] = array('#type' => 'hidden', '#attributes' => array('class' => array('filefield-source-clipboard-filename')));
     $element['filefield_clipboard']['contents'] = array('#type' => 'hidden', '#attributes' => array('class' => array('filefield-source-clipboard-contents')));
     $ajax_settings = ['url' => Url::fromRoute('file.ajax_upload'), 'options' => ['query' => ['element_parents' => implode('/', $element['#array_parents']), 'form_build_id' => $complete_form['form_build_id']['#value']]], 'wrapper' => $element['#id'] . '-ajax-wrapper', 'effect' => 'fade', 'progress' => ['type' => 'throbber', 'message' => t('Transfering file...')]];
     $element['filefield_clipboard']['upload'] = ['#name' => implode('_', $element['#parents']) . '_clipboard_upload_button', '#type' => 'submit', '#value' => t('Upload'), '#attributes' => ['class' => ['js-hide']], '#validate' => [], '#submit' => ['filefield_sources_field_submit'], '#limit_validation_errors' => [$element['#parents']], '#ajax' => $ajax_settings];
     return $element;
 }
Example #3
0
 /**
  * {@inheritdoc}
  */
 public static function process(array &$element, FormStateInterface $form_state, array &$complete_form)
 {
     $instance = entity_load('field_config', $element['#entity_type'] . '.' . $element['#bundle'] . '.' . $element['#field_name']);
     $element['filefield_imce'] = array('#weight' => 100.5, '#theme' => 'filefield_sources_element', '#source_id' => 'imce', '#filefield_source' => TRUE, '#description' => filefield_sources_element_validation_help($element['#upload_validators']));
     $filepath_id = $element['#id'] . '-imce-path';
     $display_id = $element['#id'] . '-imce-display';
     $select_id = $element['#id'] . '-imce-select';
     $element['filefield_imce']['file_path'] = array('#type' => 'textfield', '#value' => '', '#attributes' => array('id' => $filepath_id, 'onblur' => "if (this.value.length > 0) { jQuery('#{$select_id}').triggerHandler('mousedown'); }", 'style' => 'position:absolute; left: -9999em'));
     $imce_function = 'window.open(\'' . \Drupal::url('filefield_sources.imce', array('entity_type' => $element['#entity_type'], 'bundle' => $element['#bundle'], 'field_name' => $element['#field_name']), array('query' => array('app' => $instance->getLabel() . '|url@' . $filepath_id))) . '\', \'\', \'width=760,height=560,resizable=1\'); return false;';
     $element['filefield_imce']['display_path'] = array('#type' => 'markup', '#markup' => '<span id="' . $display_id . '" class="filefield-sources-imce-display">' . t('No file selected') . '</span> (<a class="filefield-sources-imce-browse" href="#" onclick="' . $imce_function . '">' . t('browse') . '</a>)');
     $ajax_settings = ['url' => Url::fromRoute('file.ajax_upload'), 'options' => ['query' => ['element_parents' => implode('/', $element['#array_parents']), 'form_build_id' => $complete_form['form_build_id']['#value']]], 'wrapper' => $element['#id'] . '-ajax-wrapper', 'effect' => 'fade'];
     $element['filefield_imce']['select'] = array('#name' => implode('_', $element['#parents']) . '_imce_select', '#type' => 'submit', '#value' => t('Select'), '#validate' => [], '#submit' => array('filefield_sources_field_submit'), '#limit_validation_errors' => [$element['#parents']], '#name' => $element['#name'] . '[filefield_imce][button]', '#id' => $select_id, '#attributes' => ['class' => ['js-hide']], '#ajax' => $ajax_settings);
     return $element;
 }
Example #4
0
 /**
  * {@inheritdoc}
  */
 public static function process(array &$element, FormStateInterface $form_state, array &$complete_form)
 {
     $settings = $element['#filefield_sources_settings']['source_attach'];
     $field_name = $element['#field_name'];
     $instance = entity_load('field_config', $element['#entity_type'] . '.' . $element['#bundle'] . '.' . $field_name);
     $element['filefield_attach'] = array('#weight' => 100.5, '#theme' => 'filefield_sources_element', '#source_id' => 'attach', '#filefield_source' => TRUE);
     $path = static::getDirectory($settings);
     $options = static::getAttachOptions($path);
     // If we have built this element before, append the list of options that we
     // had previously. This allows files to be deleted after copying them and
     // still be considered a valid option during the validation and submit.
     $triggering_element = $form_state->getTriggeringElement();
     $property = array('filefield_sources', $field_name, 'attach_options');
     if (!isset($triggering_element) && $form_state->has($property)) {
         $attach_options = $form_state->get($property);
         $options = $options + $attach_options;
     } else {
         $form_state->set(array('filefield_sources', $field_name, 'attach_options'), $options);
     }
     $description = t('This method may be used to attach files that exceed the file size limit. Files may be attached from the %directory directory on the server, usually uploaded through FTP.', array('%directory' => realpath($path)));
     // Error messages.
     if ($options === FALSE || empty($settings['path'])) {
         $attach_message = t('A file attach directory could not be located.');
         $attach_description = t('Please check your settings for the %field field.', array('%field' => $instance->getLabel()));
     } elseif (!count($options)) {
         $attach_message = t('There currently are no files to attach.');
         $attach_description = $description;
     }
     if (isset($attach_message)) {
         $element['filefield_attach']['attach_message'] = array('#markup' => $attach_message);
         $element['filefield_attach']['#description'] = $attach_description;
     } else {
         $validators = $element['#upload_validators'];
         if (isset($validators['file_validate_size'])) {
             unset($validators['file_validate_size']);
         }
         $description .= '<br />' . filefield_sources_element_validation_help($validators);
         $element['filefield_attach']['filename'] = array('#type' => 'select', '#options' => $options);
         $element['filefield_attach']['#description'] = $description;
     }
     $class = '\\Drupal\\file\\Element\\ManagedFile';
     $ajax_settings = ['callback' => [$class, 'uploadAjaxCallback'], 'options' => ['query' => ['element_parents' => implode('/', $element['#array_parents'])]], 'wrapper' => $element['#id'] . '-ajax-wrapper', 'effect' => 'fade', 'progress' => ['type' => $element['#progress_indicator'], 'message' => $element['#progress_message']]];
     $element['filefield_attach']['attach'] = ['#name' => implode('_', $element['#parents']) . '_attach', '#type' => 'submit', '#value' => t('Attach'), '#validate' => [], '#submit' => ['filefield_sources_field_submit'], '#limit_validation_errors' => [$element['#parents']], '#ajax' => $ajax_settings];
     return $element;
 }
Example #5
0
 /**
  * {@inheritdoc}
  */
 public static function process(array &$element, FormStateInterface $form_state, array &$complete_form)
 {
     $element['filefield_remote'] = array('#weight' => 100.5, '#theme' => 'filefield_sources_element', '#source_id' => 'remote', '#filefield_source' => TRUE, '#filefield_sources_hint_text' => FILEFIELD_SOURCE_REMOTE_HINT_TEXT);
     $element['filefield_remote']['url'] = array('#type' => 'textfield', '#description' => filefield_sources_element_validation_help($element['#upload_validators']), '#maxlength' => NULL);
     $ajax_settings = ['url' => Url::fromRoute('file.ajax_upload'), 'options' => ['query' => ['element_parents' => implode('/', $element['#array_parents']), 'form_build_id' => $complete_form['form_build_id']['#value']]], 'wrapper' => $element['#id'] . '-ajax-wrapper', 'effect' => 'fade', 'progress' => ['type' => 'bar', 'path' => 'file/remote/progress/' . $element['#entity_type'] . '/' . $element['#bundle'] . '/' . $element['#field_name'] . '/' . $element['#delta'], 'message' => t('Starting transfer...')]];
     $element['filefield_remote']['transfer'] = ['#name' => implode('_', $element['#parents']) . '_transfer', '#type' => 'submit', '#value' => t('Transfer'), '#validate' => array(), '#submit' => ['filefield_sources_field_submit'], '#limit_validation_errors' => [$element['#parents']], '#ajax' => $ajax_settings];
     return $element;
 }