You are here

class ImageStyleguide in Style Guide 8

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

Image styles Styleguide items implementation.

Plugin annotation


@Plugin(
  id = "image_styleguide",
  label = @Translation("Image styles Styleguide elements")
)

Hierarchy

Expanded class hierarchy of ImageStyleguide

File

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

Namespace

Drupal\styleguide\Plugin\Styleguide
View source
class ImageStyleguide extends StyleguidePluginBase {

  /**
   * The styleguide generator service.
   *
   * @var \Drupal\styleguide\Generator
   */
  protected $generator;

  /**
   * The module handler service.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * The file system service.
   *
   * @var \Drupal\Core\File\FileSystemInterface
   */
  protected $fileSystem;

  /**
   * Constructs a new imageStyleguide.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\styleguide\GeneratorInterface $styleguide_generator
   *   The styleguide generator service.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler service.
   * @param \Drupal\Core\File\FileSystemInterface $file_system
   *   The file system service.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, GeneratorInterface $styleguide_generator, ModuleHandlerInterface $module_handler, FileSystemInterface $file_system) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->generator = $styleguide_generator;
    $this->moduleHandler = $module_handler;
    $this->fileSystem = $file_system;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('styleguide.generator'), $container
      ->get('module_handler'), $container
      ->get('file_system'));
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
ImageStyleguide::$fileSystem protected property The file system service.
ImageStyleguide::$generator protected property The styleguide generator service.
ImageStyleguide::$moduleHandler protected property The module handler service.
ImageStyleguide::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
ImageStyleguide::items public function Styleguide elements implementation. Overrides StyleguideInterface::items
ImageStyleguide::__construct public function Constructs a new imageStyleguide. Overrides PluginBase::__construct
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
StyleguidePluginBase::buildLink public function Build a link.
StyleguidePluginBase::buildLinkFromRoute public function Build a link from a given route name and parameters.