Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / entity_browser / src / Plugin / Field / FieldWidget / FileBrowserWidget.php
index 7d8764576f2ea56108d1780192b82d5e0243c955..d883f1569f8f10e8724dbca6d9d3d34ee03768ea 100644 (file)
@@ -16,6 +16,7 @@ use Drupal\entity_browser\FieldWidgetDisplayManager;
 use Drupal\image\Entity\ImageStyle;
 use Symfony\Component\DependencyInjection\ContainerInterface;
 use Symfony\Component\EventDispatcher\EventDispatcherInterface;
+use Drupal\Core\Session\AccountInterface;
 
 /**
  * Entity browser file widget.
@@ -86,9 +87,11 @@ class FileBrowserWidget extends EntityReferenceBrowserWidget {
    *   The entity display repository service.
    * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
    *   The module handler service.
+   * @param \Drupal\Core\Session\AccountInterface $current_user
+   *   The current user.
    */
-  public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, array $third_party_settings, EntityTypeManagerInterface $entity_type_manager, EventDispatcherInterface $event_dispatcher, FieldWidgetDisplayManager $field_display_manager, ConfigFactoryInterface $config_factory, EntityDisplayRepositoryInterface $display_repository, ModuleHandlerInterface $module_handler) {
-    parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $third_party_settings, $entity_type_manager, $event_dispatcher, $field_display_manager, $module_handler);
+  public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, array $third_party_settings, EntityTypeManagerInterface $entity_type_manager, EventDispatcherInterface $event_dispatcher, FieldWidgetDisplayManager $field_display_manager, ConfigFactoryInterface $config_factory, EntityDisplayRepositoryInterface $display_repository, ModuleHandlerInterface $module_handler, AccountInterface $current_user) {
+    parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $third_party_settings, $entity_type_manager, $event_dispatcher, $field_display_manager, $module_handler, $current_user);
     $this->entityTypeManager = $entity_type_manager;
     $this->fieldDisplayManager = $field_display_manager;
     $this->configFactory = $config_factory;
@@ -110,7 +113,8 @@ class FileBrowserWidget extends EntityReferenceBrowserWidget {
       $container->get('plugin.manager.entity_browser.field_widget_display'),
       $container->get('config.factory'),
       $container->get('entity_display.repository'),
-      $container->get('module_handler')
+      $container->get('module_handler'),
+      $container->get('current_user')
     );
   }
 
@@ -235,7 +239,7 @@ class FileBrowserWidget extends EntityReferenceBrowserWidget {
 
     // Add the remaining columns.
     $current['#header'][] = $this->t('Metadata');
-    $current['#header'][] = ['data' => $this->t('Operations'), 'colspan' => 2];
+    $current['#header'][] = ['data' => $this->t('Operations'), 'colspan' => 3];
     $current['#header'][] = $this->t('Order', [], ['context' => 'Sort order']);
 
     /** @var \Drupal\file\FileInterface[] $entities */
@@ -243,8 +247,15 @@ class FileBrowserWidget extends EntityReferenceBrowserWidget {
       // Check to see if this entity has an edit form. If not, the edit button
       // will only throw an exception.
       if (!$entity->getEntityType()->getFormClass('edit')) {
-        $can_edit = FALSE;
+        $edit_button_access = FALSE;
       }
+      elseif ($has_file_entity) {
+        $edit_button_access = $can_edit && $entity->access('update', $this->currentUser);
+      }
+
+      // The "Replace" button will only be shown if this setting is enabled in
+      // the widget, and there is only one entity in the current selection.
+      $replace_button_access = $this->getSetting('field_widget_replace') && (count($entities) === 1);
 
       $entity_id = $entity->id();
 
@@ -349,8 +360,26 @@ class FileBrowserWidget extends EntityReferenceBrowserWidget {
           '#attributes' => [
             'data-entity-id' => $entity->getEntityTypeId() . ':' . $entity->id(),
             'data-row-id' => $delta,
+            'class' => ['edit-button'],
           ],
-          '#access' => $can_edit,
+          '#access' => $edit_button_access,
+        ],
+        'replace_button' => [
+          '#type' => 'submit',
+          '#value' => $this->t('Replace'),
+          '#ajax' => [
+            'callback' => [get_class($this), 'updateWidgetCallback'],
+            'wrapper' => $details_id,
+          ],
+          '#submit' => [[get_class($this), 'removeItemSubmit']],
+          '#name' => $field_machine_name . '_replace_' . $entity_id . '_' . md5(json_encode($field_parents)),
+          '#limit_validation_errors' => [array_merge($field_parents, [$field_machine_name, 'target_id'])],
+          '#attributes' => [
+            'data-entity-id' => $entity->getEntityTypeId() . ':' . $entity->id(),
+            'data-row-id' => $delta,
+            'class' => ['replace-button'],
+          ],
+          '#access' => $replace_button_access,
         ],
         'remove_button' => [
           '#type' => 'submit',
@@ -365,6 +394,7 @@ class FileBrowserWidget extends EntityReferenceBrowserWidget {
           '#attributes' => [
             'data-entity-id' => $entity->getEntityTypeId() . ':' . $entity->id(),
             'data-row-id' => $delta,
+            'class' => ['remove-button'],
           ],
           '#access' => (bool) $widget_settings['field_widget_remove'],
         ],
@@ -433,23 +463,27 @@ class FileBrowserWidget extends EntityReferenceBrowserWidget {
    *
    * This is a combination of logic shared between the File and Image widgets.
    *
+   * @param bool $upload
+   *   Whether or not upload-specific validators should be returned.
+   *
    * @return array
    *   An array suitable for passing to file_save_upload() or the file field
    *   element's '#upload_validators' property.
    */
-  public function getFileValidators() {
+  public function getFileValidators($upload = FALSE) {
     $validators = [];
     $settings = $this->fieldDefinition->getSettings();
 
-    // Cap the upload size according to the PHP limit.
-    $max_filesize = Bytes::toInt(file_upload_max_size());
-    if (!empty($settings['max_filesize'])) {
-      $max_filesize = min($max_filesize, Bytes::toInt($settings['max_filesize']));
+    if ($upload) {
+      // Cap the upload size according to the PHP limit.
+      $max_filesize = Bytes::toInt(file_upload_max_size());
+      if (!empty($settings['max_filesize'])) {
+        $max_filesize = min($max_filesize, Bytes::toInt($settings['max_filesize']));
+      }
+      // There is always a file size limit due to the PHP server limit.
+      $validators['file_validate_size'] = [$max_filesize];
     }
 
-    // There is always a file size limit due to the PHP server limit.
-    $validators['file_validate_size'] = [$max_filesize];
-
     // Images have expected defaults for file extensions.
     // See \Drupal\image\Plugin\Field\FieldWidget::formElement() for details.
     if ($this->fieldDefinition->getType() == 'image') {
@@ -458,16 +492,16 @@ class FileBrowserWidget extends EntityReferenceBrowserWidget {
       $extensions = isset($settings['file_extensions']) ? $settings['file_extensions'] : implode(' ', $supported_extensions);
       $extensions = array_intersect(explode(' ', $extensions), $supported_extensions);
       $validators['file_validate_extensions'] = [implode(' ', $extensions)];
+
+      // Add resolution validation.
+      if (!empty($settings['max_resolution']) || !empty($settings['min_resolution'])) {
+        $validators['entity_browser_file_validate_image_resolution'] = [$settings['max_resolution'], $settings['min_resolution']];
+      }
     }
     elseif (!empty($settings['file_extensions'])) {
       $validators['file_validate_extensions'] = [$settings['file_extensions']];
     }
 
-    // Add upload resolution validation.
-    if ($settings['max_resolution'] || $settings['min_resolution']) {
-      $validators['entity_browser_file_validate_image_resolution'] = [$settings['max_resolution'], $settings['min_resolution']];
-    }
-
     return $validators;
   }
 
@@ -479,9 +513,9 @@ class FileBrowserWidget extends EntityReferenceBrowserWidget {
     $settings = $this->fieldDefinition->getSettings();
     // Add validators based on our current settings.
     $data['validators']['file'] = ['validators' => $this->getFileValidators()];
-    // Provide context for widgets to enhance their configuration. Currently
-    // we only know that "upload_location" is used.
+    // Provide context for widgets to enhance their configuration.
     $data['widget_context']['upload_location'] = $settings['uri_scheme'] . '://' . $settings['file_directory'];
+    $data['widget_context']['upload_validators'] = $this->getFileValidators(TRUE);
     return $data;
   }