You are here

public static function Imce::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/Imce.php, line 74

Class

Imce
A FileField source plugin to allow referencing of files from IMCE.

Namespace

Drupal\filefield_sources\Plugin\FilefieldSource

Code

public static function process(array &$element, FormStateInterface $form_state, array &$complete_form) {
  $instance = \Drupal::entityTypeManager()
    ->getStorage('field_config')
    ->load($element['#entity_type'] . '.' . $element['#bundle'] . '.' . $element['#field_name']);
  $element['filefield_imce'] = [
    '#weight' => 100.5,
    '#theme' => 'filefield_sources_element',
    '#source_id' => 'imce',
    // Required for proper theming.
    '#filefield_source' => TRUE,
    '#description' => filefield_sources_element_validation_help($element['#upload_validators']),
  ];
  $imce_url = Url::fromRoute('filefield_sources.imce', [
    'entity_type' => $element['#entity_type'],
    'bundle_name' => $element['#bundle'],
    'field_name' => $element['#field_name'],
  ], [
    'query' => [
      'sendto' => 'imceFileField.sendto',
      'fieldId' => $element['#attributes']['data-drupal-selector'] . '-filefield-imce',
    ],
  ])
    ->toString();
  $element['filefield_imce']['browse'] = [
    '#type' => 'markup',
    '#markup' => '<span>' . t('No file selected') . '</span> (<a class="filefield-sources-imce-browse" href="' . $imce_url . '">' . t('browse') . '</a>)',
  ];
  $element['#attached']['library'][] = 'imce/drupal.imce.filefield';

  // Set the pre-renderer to conditionally disable the elements.
  $element['#pre_render'][] = [
    get_called_class(),
    'preRenderWidget',
  ];

  // Path input.
  $element['filefield_imce']['imce_paths'] = [
    '#type' => 'hidden',
    // Reset value to prevent consistent errors.
    '#value' => '',
  ];
  $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_imce']['imce_button'] = [
    '#name' => implode('_', $element['#parents']) . '_imce_select',
    '#type' => 'submit',
    '#value' => t('Select'),
    '#attributes' => [
      'class' => [
        'js-hide',
      ],
    ],
    '#validate' => [],
    '#submit' => [
      'filefield_sources_field_submit',
    ],
    '#limit_validation_errors' => [
      $element['#parents'],
    ],
    '#ajax' => $ajax_settings,
  ];
  return $element;
}