class IconDefinition

Handle an icon definition.

@internal This API is experimental.

Hierarchy

Expanded class hierarchy of IconDefinition

11 files declare their use of IconDefinition
ExtractorTest.php in core/tests/Drupal/Tests/Core/Theme/Icon/Plugin/ExtractorTest.php
Icon.php in core/lib/Drupal/Core/Render/Element/Icon.php
IconDefinitionTest.php in core/tests/Drupal/Tests/Core/Theme/Icon/IconDefinitionTest.php
IconTest.php in core/tests/Drupal/Tests/Core/Theme/Icon/IconTest.php
IconTestTrait.php in core/tests/Drupal/Tests/Core/Theme/Icon/IconTestTrait.php

... See full list

File

core/lib/Drupal/Core/Theme/Icon/IconDefinition.php, line 17

Namespace

Drupal\Core\Theme\Icon
View source
class IconDefinition implements IconDefinitionInterface {
    public const ICON_SEPARATOR = ':';
    
    /**
     * Constructor for IconDefinition.
     *
     * @param string $pack_id
     *   The id of the icon pack.
     * @param string $icon_id
     *   The id of the icon.
     * @param string $template
     *   The template of the icon.
     * @param string|null $source
     *   The source, url or path of the icon.
     * @param string|null $group
     *   The group of the icon.
     * @param array $data
     *   The additional data of the icon.
     */
    private function __construct(string $pack_id, string $icon_id, string $template, ?string $source, ?string $group, array $data) {
    }
    
    /**
     * {@inheritdoc}
     */
    public static function create(string $pack_id, string $icon_id, string $template, ?string $source = NULL, ?string $group = NULL, array $data = []) : self {
        $errors = [];
        if (0 === strlen($pack_id)) {
            $errors[] = 'Empty pack_id provided!';
        }
        if (0 === strlen($icon_id)) {
            $errors[] = 'Empty icon_id provided!';
        }
        if (0 === strlen($template)) {
            $errors[] = 'Empty template provided!';
        }
        if (count($errors)) {
            throw new IconDefinitionInvalidDataException(implode(' ', $errors));
        }
        // Cleanup of data that do not need to be passed.
        unset($data['config']['sources'], $data['relative_path'], $data['absolute_path']);
        return new self($pack_id, $icon_id, $template, $source, $group, $data);
    }
    
    /**
     * {@inheritdoc}
     */
    public static function createIconId(string $pack_id, string $icon_id) : string {
        return sprintf('%s%s%s', $pack_id, self::ICON_SEPARATOR, $icon_id);
    }
    
    /**
     * {@inheritdoc}
     */
    public static function getIconDataFromId(string $icon_full_id) : ?array {
        $icon_data = explode(self::ICON_SEPARATOR, $icon_full_id, 2);
        if (count($icon_data) < 2) {
            return NULL;
        }
        return [
            'pack_id' => $icon_data[0],
            'icon_id' => $icon_data[1],
        ];
    }
    
    /**
     * {@inheritdoc}
     */
    public static function getRenderable(string $icon_full_id, array $settings = []) : ?array {
        if (!($icon_data = self::getIconDataFromId($icon_full_id))) {
            return NULL;
        }
        if (isset($settings[$icon_data['pack_id']])) {
            $settings = $settings[$icon_data['pack_id']];
        }
        return [
            '#type' => 'icon',
            '#pack_id' => $icon_data['pack_id'],
            '#icon_id' => $icon_data['icon_id'],
            '#settings' => $settings,
        ];
    }
    
    /**
     * {@inheritdoc}
     */
    public function getLabel() : string {
        return self::humanize($this->icon_id);
    }
    
    /**
     * {@inheritdoc}
     */
    public function getId() : string {
        return sprintf('%s%s%s', $this->pack_id, self::ICON_SEPARATOR, $this->icon_id);
    }
    
    /**
     * {@inheritdoc}
     */
    public function getIconId() : string {
        return $this->icon_id;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getPackId() : string {
        return $this->pack_id;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getSource() : ?string {
        return $this->source;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getGroup() : ?string {
        return $this->group;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getTemplate() : string {
        return $this->template;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getPackLabel() : ?TranslatableMarkup {
        return $this->data['label'] ?? NULL;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getLibrary() : ?string {
        return $this->data['library'] ?? NULL;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getAllData() : array {
        return $this->data;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getData(string $key) : mixed {
        return $this->data[$key] ?? NULL;
    }
    
    /**
     * Humanize a text for admin display.
     *
     * @param string $text
     *   The text to humanize.
     *
     * @return string
     *   The human friendly text.
     */
    public static function humanize(string $text) : string {
        return (string) u($text)->snake()
            ->replace('_', ' ')
            ->title(allWords: TRUE);
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
IconDefinition::create public static function Create an icon definition. Overrides IconDefinitionInterface::create
IconDefinition::createIconId public static function Create an icon full id. Overrides IconDefinitionInterface::createIconId
IconDefinition::getAllData public function Get all icon data. Overrides IconDefinitionInterface::getAllData
IconDefinition::getData public function Get a specific icon data. Overrides IconDefinitionInterface::getData
IconDefinition::getGroup public function Get the icon Group. Overrides IconDefinitionInterface::getGroup
IconDefinition::getIconDataFromId public static function Get icon id and pack id from an icon full id. Overrides IconDefinitionInterface::getIconDataFromId
IconDefinition::getIconId public function Get the icon id. Overrides IconDefinitionInterface::getIconId
IconDefinition::getId public function Get the full icon id. Overrides IconDefinitionInterface::getId
IconDefinition::getLabel public function Get the icon label as human friendly. Overrides IconDefinitionInterface::getLabel
IconDefinition::getLibrary public function Get the icon Twig library. Overrides IconDefinitionInterface::getLibrary
IconDefinition::getPackId public function Get the icon Pack id. Overrides IconDefinitionInterface::getPackId
IconDefinition::getPackLabel public function Get the icon pack label. Overrides IconDefinitionInterface::getPackLabel
IconDefinition::getRenderable public static function Get the icon renderable element array. Overrides IconDefinitionInterface::getRenderable
IconDefinition::getSource public function Get the icon source, path or url. Overrides IconDefinitionInterface::getSource
IconDefinition::getTemplate public function Get the icon Twig template. Overrides IconDefinitionInterface::getTemplate
IconDefinition::humanize public static function Humanize a text for admin display.
IconDefinition::ICON_SEPARATOR public constant
IconDefinition::__construct private function Constructor for IconDefinition.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.