You are here

class Imce in FileField Sources 8

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

Plugin annotation


@FilefieldSource(
  id = "imce",
  name = @Translation("IMCE file browser"),
  label = @Translation("File browser"),
  description = @Translation("Select a file to use from a file browser."),
  weight = -1
)

Hierarchy

Expanded class hierarchy of Imce

File

src/Plugin/FilefieldSource/Imce.php, line 25

Namespace

Drupal\filefield_sources\Plugin\FilefieldSource
View source
class Imce implements FilefieldSourceInterface, TrustedCallbackInterface {

  /**
   * {@inheritdoc}
   */
  public static function value(array &$element, &$input, FormStateInterface $form_state) {
    if (isset($input['filefield_imce']['imce_paths']) && $input['filefield_imce']['imce_paths'] != '') {
      $instance = \Drupal::entityTypeManager()
        ->getStorage('field_config')
        ->load($element['#entity_type'] . '.' . $element['#bundle'] . '.' . $element['#field_name']);
      $field_settings = $instance
        ->getSettings();
      $scheme = $field_settings['uri_scheme'];
      $imce_paths = explode(':', $input['filefield_imce']['imce_paths']);
      $uris = [];
      foreach ($imce_paths as $imce_path) {

        // $wrapper = \Drupal::service('stream_wrapper_manager')->getViaScheme($scheme);
        // $file_directory_prefix = $scheme == 'private' ? 'system/files' : $wrapper->getDirectoryPath();
        // $uri = preg_replace('/^' . preg_quote(base_path() . $file_directory_prefix . '/', '/') . '/', $scheme . '://', $imce_path);.
        $uri = rawurldecode($scheme . '://' . $imce_path);
        $uris[] = $uri;
      }

      // Resolve the file path to an FID.
      $connection = \Drupal::service('database');
      $fids = $connection
        ->select('file_managed', 'f')
        ->condition('uri', $uris, 'IN')
        ->fields('f', [
        'fid',
      ])
        ->execute()
        ->fetchCol();
      if ($fids) {
        $files = File::loadMultiple($fids);
        foreach ($files as $file) {
          if (filefield_sources_element_validate($element, $file, $form_state)) {
            if (!in_array($file
              ->id(), $input['fids'])) {
              $input['fids'][] = $file
                ->id();
            }
          }
        }
      }
      else {
        $form_state
          ->setError($element, t('The selected file could not be used because the file does not exist in the database.'));
      }

      // No matter what happens, clear the value from the file path field.
      $input['filefield_imce']['imce_paths'] = '';
    }
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * Theme the output of the imce element.
   */
  public static function element($variables) {
    $element = $variables['element'];
    $output = '';
    foreach (Element::children($element) as $key) {
      if (!empty($element[$key])) {
        $output .= \Drupal::service('renderer')
          ->render($element[$key]);
      }
    }
    return '<div class="filefield-source filefield-source-imce clear-block">' . $output . '</div>';
  }

  /**
   * Define routes for Imce source.
   *
   * @return array
   *   Array of routes.
   */
  public static function routes() {
    $routes = [];
    $routes['filefield_sources.imce'] = new Route('/file/imce/{entity_type}/{bundle_name}/{field_name}', [
      '_controller' => '\\Drupal\\filefield_sources\\Controller\\ImceController::page',
      '_title' => 'File Manager',
    ], [
      '_access_filefield_sources_field' => 'TRUE',
    ]);
    return $routes;
  }

  /**
   * Implements hook_filefield_source_settings().
   */
  public static function settings(WidgetInterface $plugin) {
    $settings = $plugin
      ->getThirdPartySetting('filefield_sources', 'filefield_sources', [
      'source_imce' => [
        'imce_mode' => 0,
      ],
    ]);
    $return['source_imce'] = [
      '#title' => t('IMCE file browser settings'),
      '#type' => 'details',
      '#access' => \Drupal::moduleHandler()
        ->moduleExists('imce'),
    ];

    // $imce_admin_url = \Drupal::url('imce.admin');.
    $imce_admin_url = 'admin/config/media/imce';
    $return['source_imce']['imce_mode'] = [
      '#type' => 'radios',
      '#title' => t('File browser mode'),
      '#options' => [
        0 => t('Restricted: Users can only browse the field directory. No file operations are allowed.'),
        1 => t('Full: Browsable directories are defined by <a href=":imce-admin-url">IMCE configuration profiles</a>. File operations are allowed.', [
          ':imce-admin-url' => $imce_admin_url,
        ]),
      ],
      '#default_value' => isset($settings['source_imce']['imce_mode']) ? $settings['source_imce']['imce_mode'] : 0,
    ];
    return $return;
  }

  /**
   * Pre-renders widget form.
   */
  public static function preRenderWidget($element) {

    // Hide elements if there is already an uploaded file.
    if (!empty($element['#value']['fids'])) {
      $element['filefield_imce']['imce_paths']['#access'] = FALSE;
      $element['filefield_imce']['imce_button']['#access'] = FALSE;
    }
    return $element;
  }

  /**
   * {@inheritdoc}
   */
  public static function trustedCallbacks() {
    return [
      'preRenderWidget',
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Imce::element public static function Theme the output of the imce element.
Imce::preRenderWidget public static function Pre-renders widget form.
Imce::process public static function Process callback for file field source plugin. Overrides FilefieldSourceInterface::process
Imce::routes public static function Define routes for Imce source.
Imce::settings public static function Implements hook_filefield_source_settings().
Imce::trustedCallbacks public static function Lists the trusted callbacks provided by the implementing class. Overrides TrustedCallbackInterface::trustedCallbacks
Imce::value public static function Value callback for file field source plugin. Overrides FilefieldSourceInterface::value
TrustedCallbackInterface::THROW_EXCEPTION constant Untrusted callbacks throw exceptions.
TrustedCallbackInterface::TRIGGER_SILENCED_DEPRECATION constant Untrusted callbacks trigger silenced E_USER_DEPRECATION errors.
TrustedCallbackInterface::TRIGGER_WARNING constant Untrusted callbacks trigger E_USER_WARNING errors.