You are here

public function ImageEmbedBlock::browserForm in File Entity Browser 8

Constructs parts of the form needed to use Entity Browser.

Parameters

array $files: An array representing the current configuration + form state.

Return value

array A render array representing Entity Browser components.

1 call to ImageEmbedBlock::browserForm()
ImageEmbedBlock::blockForm in src/Plugin/Block/ImageEmbedBlock.php

File

src/Plugin/Block/ImageEmbedBlock.php, line 77

Class

ImageEmbedBlock
Provides the "Image Embed" block.

Namespace

Drupal\file_browser\Plugin\Block

Code

public function browserForm(array $files) {
  $selection = [
    '#type' => 'container',
    '#attributes' => [
      'id' => 'image-embed-block-browser',
    ],
  ];
  $selection['fids'] = [
    '#type' => 'entity_browser',
    '#entity_browser' => 'browse_files_modal',
    '#entity_browser_validators' => [
      'entity_type' => [
        'type' => 'file',
      ],
    ],
    '#process' => [
      [
        '\\Drupal\\entity_browser\\Element\\EntityBrowserElement',
        'processEntityBrowser',
      ],
      [
        get_called_class(),
        'processEntityBrowser',
      ],
    ],
  ];
  $order_class = 'image-embed-block-delta-order';
  $selection['table'] = [
    '#type' => 'table',
    '#header' => [
      $this
        ->t('Preview'),
      $this
        ->t('Filename'),
      $this
        ->t('Metadata'),
      $this
        ->t('Order', [], [
        'context' => 'Sort order',
      ]),
    ],
    '#empty' => $this
      ->t('No files yet'),
    '#tabledrag' => [
      [
        'action' => 'order',
        'relationship' => 'sibling',
        'group' => $order_class,
      ],
    ],
  ];
  $delta = 0;
  foreach ($files as $info) {
    $file = File::load($info['fid']);
    $uri = $file
      ->getFileUri();
    $image = \Drupal::service('image.factory')
      ->get($uri);
    if ($image
      ->isValid()) {
      $width = $image
        ->getWidth();
      $height = $image
        ->getHeight();
    }
    else {
      $width = $height = NULL;
    }
    $display = [
      '#theme' => 'image_style',
      '#width' => $width,
      '#height' => $height,
      '#style_name' => 'file_entity_browser_small',
      '#uri' => $uri,
    ];
    $fid = $file
      ->id();
    $selection['table'][$fid] = [
      '#attributes' => [
        'class' => [
          'draggable',
        ],
        'data-entity-id' => $file
          ->getEntityTypeId() . ':' . $fid,
      ],
      'display' => $display,
      'filename' => [
        '#markup' => $file
          ->label(),
      ],
      'alt' => [
        '#type' => 'textfield',
        '#title' => $this
          ->t('Alternative text'),
        '#default_value' => isset($info['settings']['alt']) ? $info['settings']['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.'),
      ],
      '_weight' => [
        '#type' => 'weight',
        '#title' => $this
          ->t('Weight for row @number', [
          '@number' => $delta + 1,
        ]),
        '#title_display' => 'invisible',
        '#delta' => count($files),
        '#default_value' => $delta,
        '#attributes' => [
          'class' => [
            $order_class,
          ],
        ],
      ],
    ];
    $delta++;
  }
  return $selection;
}