You are here

function managed_file_element_process in Managed File 7

Process widget of managed file.

@internal

Parameters

array $element: Form element to process.

Return value

array Processed element.

See also

managed_file_element_info_alter()

1 string reference to 'managed_file_element_process'
managed_file_element_info_alter in ./managed_file.module
Implements hook_element_info_alter().

File

includes/managed_file.common.inc, line 61
Auxiliary functionality.

Code

function managed_file_element_process(array $element) {
  if (!empty($element['#widget'])) {
    $widget = $element['#widget'];
    $widgets = managed_file_element_widgets();
    if (isset($widgets[$widget])) {
      $scheme_name = file_uri_scheme($element['#upload_location']);

      /* @var \DrupalLocalStreamWrapper $stream_wrapper */
      $stream_wrapper = file_stream_wrapper_get_instance_by_scheme($scheme_name);
      if (FALSE !== $stream_wrapper) {
        global $language;
        $scheme_url = $stream_wrapper
          ->getDirectoryPath();
        $module_path = drupal_get_path('module', 'managed_file');
        $widget_path = $widgets[$widget]['path'];
        $settings = [
          'basePath' => DRUPAL_ROOT . '/' . $widget_path,
          'location' => $element['#upload_location'],
          'language' => $language->language,
          'schemeURL' => $scheme_url,
          'schemeName' => $scheme_name,
        ];
        $settings_key = md5(serialize($settings));

        // Pass only unique settings.
        $element['#attached']['js'] = [
          // Widget library.
          [
            'data' => "{$widget_path}/{$widget}.js",
            'weight' => -1002,
          ],
          // File Manager.
          [
            'data' => "{$module_path}/js/file.js",
            'weight' => -1001,
          ],
          // Implementation of one of file managers.
          [
            'data' => "{$module_path}/js/file.{$widget}.js",
            'weight' => -1000,
          ],
          // Widget-related settings.
          [
            'type' => 'setting',
            'data' => [
              $settings_key => $settings,
            ],
          ],
        ];

        // @todo Unable to pass "data-*" or another one attribute.
        $element['#attributes']['class'][$settings_key] = "{$widget}-file-manager {$settings_key}";
      }
    }
  }
  if ($element['#preview']) {
    $element['#pre_render'][] = 'managed_file_element_preview';
  }
  if ($element['#styles']) {
    $element['style'] = [
      '#type' => 'select',
      '#title' => t('Image style'),
      '#options' => image_style_options(),
      '#default_value' => isset($element['#default_value']['style']) ? $element['#default_value']['style'] : '',
    ];
  }
  return $element;
}