You are here

protected function FileBrowserWidget::displayCurrentSelection in Entity Browser 8.2

Same name and namespace in other branches
  1. 8 src/Plugin/Field/FieldWidget/FileBrowserWidget.php \Drupal\entity_browser\Plugin\Field\FieldWidget\FileBrowserWidget::displayCurrentSelection()

Builds the render array for displaying the current results.

Parameters

string $details_id: The ID for the details element.

string[] $field_parents: Field parents.

\Drupal\Core\Entity\ContentEntityInterface[] $entities: Array of referenced entities.

Return value

array The render array for the current selection.

Overrides EntityReferenceBrowserWidget::displayCurrentSelection

File

src/Plugin/Field/FieldWidget/FileBrowserWidget.php, line 216

Class

FileBrowserWidget
Entity browser file widget.

Namespace

Drupal\entity_browser\Plugin\Field\FieldWidget

Code

protected function displayCurrentSelection($details_id, array $field_parents, array $entities) {
  $field_type = $this->fieldDefinition
    ->getType();
  $field_settings = $this->fieldDefinition
    ->getSettings();
  $field_machine_name = $this->fieldDefinition
    ->getName();
  $file_settings = $this->configFactory
    ->get('file.settings');
  $widget_settings = $this
    ->getSettings();
  $view_mode = $widget_settings['view_mode'];
  $can_edit = (bool) $widget_settings['field_widget_edit'];
  $has_file_entity = $this->moduleHandler
    ->moduleExists('file_entity');
  $delta = 0;
  $order_class = $field_machine_name . '-delta-order';
  $current = [
    '#type' => 'table',
    '#empty' => $this
      ->t('No files yet'),
    '#attributes' => [
      'class' => [
        'entities-list',
      ],
    ],
    '#tabledrag' => [
      [
        'action' => 'order',
        'relationship' => 'sibling',
        'group' => $order_class,
      ],
    ],
  ];
  if ($has_file_entity || $field_type == 'image' && !empty($widget_settings['preview_image_style'])) {

    // Had the preview column if we have one.
    $current['#header'][] = $this
      ->t('Preview');
  }

  // Add the filename if there is no view builder.
  if (!$has_file_entity) {
    $current['#header'][] = $this
      ->t('Filename');
  }

  // Add the remaining columns.
  $current['#header'][] = $this
    ->t('Metadata');
  $current['#header'][] = [
    'data' => $this
      ->t('Operations'),
    'colspan' => 3,
  ];
  $current['#header'][] = $this
    ->t('Order', [], [
    'context' => 'Sort order',
  ]);

  /** @var \Drupal\file\FileInterface[] $entities */
  foreach ($entities as $entity) {

    // 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')) {
      $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();

    // Find the default description.
    $description = '';
    $display_field = $field_settings['display_default'];
    $alt = '';
    $title = '';
    $weight = $delta;
    $width = NULL;
    $height = NULL;
    foreach ($this->items as $item) {
      if ($item->target_id == $entity_id) {
        if ($field_type == 'file') {
          $description = $item->description;
          $display_field = $item->display;
        }
        elseif ($field_type == 'image') {
          $alt = $item->alt;
          $title = $item->title;
          $width = $item->width;
          $height = $item->height;
        }
        $weight = $item->_weight ?: $delta;
      }
    }
    $current[$entity_id] = [
      '#attributes' => [
        'class' => [
          'draggable',
        ],
        'data-entity-id' => $entity
          ->getEntityTypeId() . ':' . $entity_id,
        'data-row-id' => $delta,
      ],
    ];

    // Provide a rendered entity if a view builder is available.
    if ($has_file_entity) {
      $current[$entity_id]['display'] = $this->entityTypeManager
        ->getViewBuilder('file')
        ->view($entity, $view_mode);
    }
    elseif ($field_type == 'image' && !empty($widget_settings['preview_image_style'])) {
      $uri = $entity
        ->getFileUri();
      $current[$entity_id]['display'] = [
        '#weight' => -10,
        '#theme' => 'image_style',
        '#width' => $width,
        '#height' => $height,
        '#style_name' => $widget_settings['preview_image_style'],
        '#uri' => $uri,
      ];
    }

    // Assume that the file name is part of the preview output if
    // file entity is installed, do not show this column in that case.
    if (!$has_file_entity) {
      $current[$entity_id]['filename'] = [
        '#markup' => $entity
          ->label(),
      ];
    }
    $current[$entity_id] += [
      'meta' => [
        'display_field' => [
          '#type' => 'checkbox',
          '#title' => $this
            ->t('Include file in display'),
          '#default_value' => (bool) $display_field,
          '#access' => $field_type == 'file' && $field_settings['display_field'],
        ],
        'description' => [
          '#type' => $file_settings
            ->get('description.type'),
          '#title' => $this
            ->t('Description'),
          '#default_value' => $description,
          '#size' => 45,
          '#maxlength' => $file_settings
            ->get('description.length'),
          '#description' => $this
            ->t('The description may be used as the label of the link to the file.'),
          '#access' => $field_type == 'file' && $field_settings['description_field'],
        ],
        'alt' => [
          '#type' => 'textfield',
          '#title' => $this
            ->t('Alternative text'),
          '#default_value' => $alt,
          '#size' => 45,
          '#maxlength' => 512,
          '#description' => $this
            ->t('This text will be used by screen readers, search engines, or when the image cannot be loaded.'),
          '#access' => $field_type == 'image' && $field_settings['alt_field'],
          '#required' => $field_type == 'image' && $field_settings['alt_field_required'],
        ],
        'title' => [
          '#type' => 'textfield',
          '#title' => $this
            ->t('Title'),
          '#default_value' => $title,
          '#size' => 45,
          '#maxlength' => 1024,
          '#description' => $this
            ->t('The title is used as a tool tip when the user hovers the mouse over the image.'),
          '#access' => $field_type == 'image' && $field_settings['title_field'],
          '#required' => $field_type == 'image' && $field_settings['title_field_required'],
        ],
      ],
      'edit_button' => [
        '#type' => 'submit',
        '#value' => $this
          ->t('Edit'),
        '#ajax' => [
          'url' => Url::fromRoute('entity_browser.edit_form', [
            'entity_type' => $entity
              ->getEntityTypeId(),
            'entity' => $entity_id,
          ]),
          'options' => [
            'query' => [
              'details_id' => $details_id,
            ],
          ],
        ],
        '#attributes' => [
          'data-entity-id' => $entity
            ->getEntityTypeId() . ':' . $entity
            ->id(),
          'data-row-id' => $delta,
          'class' => [
            'edit-button',
          ],
        ],
        '#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',
        '#value' => $this
          ->t('Remove'),
        '#ajax' => [
          'callback' => [
            get_class($this),
            'updateWidgetCallback',
          ],
          'wrapper' => $details_id,
        ],
        '#submit' => [
          [
            get_class($this),
            'removeItemSubmit',
          ],
        ],
        '#name' => $field_machine_name . '_remove_' . $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' => [
            'remove-button',
          ],
        ],
        '#access' => (bool) $widget_settings['field_widget_remove'],
      ],
      '_weight' => [
        '#type' => 'weight',
        '#title' => $this
          ->t('Weight for row @number', [
          '@number' => $delta + 1,
        ]),
        '#title_display' => 'invisible',
        // Note: this 'delta' is the FAPI #type 'weight' element's property.
        '#delta' => count($entities),
        '#default_value' => $weight,
        '#attributes' => [
          'class' => [
            $order_class,
          ],
        ],
      ],
    ];
    $current['#attached']['library'][] = 'entity_browser/file_browser';
    $delta++;
  }
  return $current;
}