You are here

public static function Reference::process in FileField Sources 8

Process callback for file field source plugin.

Parameters

array $element: An associative array containing the properties and children of the generic input element.

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

array $complete_form: The complete form structure.

Return value

array The processed element.

Overrides FilefieldSourceInterface::process

File

src/Plugin/FilefieldSource/Reference.php, line 67

Class

Reference
A FileField source plugin to allow referencing of existing files.

Namespace

Drupal\filefield_sources\Plugin\FilefieldSource

Code

public static function process(array &$element, FormStateInterface $form_state, array &$complete_form) {
  $element['filefield_reference'] = [
    '#weight' => 100.5,
    '#theme' => 'filefield_sources_element',
    '#source_id' => 'reference',
    // Required for proper theming.
    '#filefield_source' => TRUE,
    '#filefield_sources_hint_text' => FILEFIELD_SOURCE_REFERENCE_HINT_TEXT,
  ];
  $autocomplete_route_parameters = [
    'entity_type' => $element['#entity_type'],
    'bundle_name' => $element['#bundle'],
    'field_name' => $element['#field_name'],
  ];
  $element['filefield_reference']['autocomplete'] = [
    '#type' => 'textfield',
    '#autocomplete_route_name' => 'filefield_sources.autocomplete',
    '#autocomplete_route_parameters' => $autocomplete_route_parameters,
    '#description' => filefield_sources_element_validation_help($element['#upload_validators']),
  ];
  $class = '\\Drupal\\file\\Element\\ManagedFile';
  $ajax_settings = [
    'callback' => [
      $class,
      'uploadAjaxCallback',
    ],
    'options' => [
      'query' => [
        'element_parents' => implode('/', $element['#array_parents']),
      ],
    ],
    'wrapper' => $element['upload_button']['#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;
}