You are here

protected function FileMatcher::buildDescription in Linkit 8.5

Same name and namespace in other branches
  1. 8.4 src/Plugin/Linkit/Matcher/FileMatcher.php \Drupal\linkit\Plugin\Linkit\Matcher\FileMatcher::buildDescription()

Builds the metadata string used in the suggestion.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The matched entity.

Return value

string The metadata for this entity.

Overrides EntityMatcher::buildDescription

File

src/Plugin/Linkit/Matcher/FileMatcher.php, line 185

Class

FileMatcher
Provides specific linkit matchers for the file entity type.

Namespace

Drupal\linkit\Plugin\Linkit\Matcher

Code

protected function buildDescription(EntityInterface $entity) {
  $description_array = [];
  $description_array[] = parent::buildDescription($entity);

  /** @var \Drupal\file\FileInterface $entity */
  $file = $entity
    ->getFileUri();
  if ($this->configuration['images']['show_dimensions'] || $this->configuration['images']['show_thumbnail']) {
    $image_factory = \Drupal::service('image.factory');
    $supported_extensions = $image_factory
      ->getSupportedExtensions();

    // Check if the file extension is supported by the image toolkit.
    if (empty(file_validate_extensions($entity, implode(' ', $supported_extensions)))) {

      /** @var \Drupal\Core\Image\ImageInterface $image */
      $image = $image_factory
        ->get($file);
      if ($image
        ->isValid()) {
        if ($this->configuration['images']['show_dimensions']) {
          $description_array[] = $image
            ->getWidth() . 'x' . $image
            ->getHeight() . 'px';
        }
        if ($this->configuration['images']['show_thumbnail'] && $this->moduleHandler
          ->moduleExists('image')) {
          $image_element = [
            '#weight' => -10,
            '#theme' => 'image_style',
            '#style_name' => $this->configuration['images']['thumbnail_image_style'],
            '#uri' => $entity
              ->getFileUri(),
          ];
          $description_array[] = (string) \Drupal::service('renderer')
            ->render($image_element);
        }
      }
    }
  }
  $description = implode('<br />', $description_array);
  return LinkitXss::descriptionFilter($description);
}