You are here

public function ImageStyleguide::items in Style Guide 8

Same name and namespace in other branches
  1. 2.x src/Plugin/Styleguide/ImageStyleguide.php \Drupal\styleguide\Plugin\Styleguide\ImageStyleguide::items()

Styleguide elements implementation.

Return value

array An array of Styleguide elements.

Overrides StyleguideInterface::items

File

src/Plugin/Styleguide/ImageStyleguide.php, line 85

Class

ImageStyleguide
Image styles Styleguide items implementation.

Namespace

Drupal\styleguide\Plugin\Styleguide

Code

public function items() {
  $items = [];
  if ($this->moduleHandler
    ->moduleExists('image')) {

    // Get the sample file provided by the module.
    $preview_img_path = 'public://styleguide-preview.jpg';
    if ($this->fileSystem
      ->getDestinationFilename($preview_img_path, FileSystemInterface::EXISTS_ERROR) !== FALSE) {

      // Move the image so that styles may be applied.
      $this->fileSystem
        ->copy($this->generator
        ->image('vertical'), $preview_img_path, FileSystemInterface::EXISTS_ERROR);
    }

    // Iterate through the image styles on the site.
    foreach (ImageStyle::loadMultiple() as $stylename => $style) {
      $details = [];
      foreach ($style
        ->getEffects() as $effect) {
        $summary = $effect
          ->getSummary();
        $summary = render($summary);
        $label = $effect
          ->label();
        if ($summary) {
          $details[] = new FormattableMarkup('%label: @summary', [
            '%label' => $label,
            '@summary' => $summary,
          ]);
        }
        else {
          $details[] = new FormattableMarkup('%label', [
            '%label' => $label,
          ]);
        }
      }
      $title = $this
        ->t('Image style, @stylename', [
        '@stylename' => $style
          ->get('label'),
      ]);
      $items['image_' . $stylename] = [
        'title' => $title,
        'description' => [
          '#theme' => 'item_list',
          '#items' => $details,
        ],
        'content' => [
          '#theme' => 'image_style',
          '#uri' => $preview_img_path,
          '#style_name' => $stylename,
          '#alt' => $title,
          '#title' => $title,
        ],
        'group' => $this
          ->t('Media'),
      ];
    }
  }
  return $items;
}