You are here

class MiconIcon in Micon 8

Same name and namespace in other branches
  1. 2.x src/MiconIcon.php \Drupal\micon\MiconIcon

Defines the Micon icon.

Hierarchy

Expanded class hierarchy of MiconIcon

1 file declares its use of MiconIcon
Micon.php in src/Entity/Micon.php

File

src/MiconIcon.php, line 12

Namespace

Drupal\micon
View source
class MiconIcon implements MiconIconInterface, RenderableInterface {

  /**
   * The Micon type. Either 'font' or 'image'.
   *
   * @var string
   */
  protected $type;

  /**
   * The Micon icon data.
   *
   * @var array
   */
  protected $data;

  /**
   * The Attribute object.
   *
   * @var \Drupal\Core\Template\Attribute
   */
  protected $attributes;

  /**
   * Constructs a new MiconIcon.
   *
   * @param string $type
   *   The type of icon. Either 'font' or 'image'.
   * @param array $data
   *   The icon data array provided from the Micon package info file.
   * @param array $attributes
   *   The attributes to add to the group wrapper.
   */
  public function __construct($type, array $data, array $attributes = []) {
    $this->type = $type;
    $this->data = $data;
    $this
      ->setAttributes($attributes);
  }

  /**
   * {@inheritdoc}
   */
  public function getType() {
    return $this->type;
  }

  /**
   * {@inheritdoc}
   */
  public function getPackageId() {
    return $this->data['package_id'];
  }

  /**
   * {@inheritdoc}
   */
  public function getPackageLabel() {
    return $this->data['package_label'];
  }

  /**
   * {@inheritdoc}
   */
  public function getPrefix() {
    return $this->data['prefix'];
  }

  /**
   * {@inheritdoc}
   */
  public function getTag() {
    return $this->data['tag'];
  }

  /**
   * {@inheritdoc}
   */
  public function getSelector() {
    return $this
      ->getPrefix() . $this
      ->getTag();
  }

  /**
   * {@inheritdoc}
   */
  public function getHex() {
    return $this->type == 'font' ? '\\' . dechex($this->data['properties']['code']) : '';
  }

  /**
   * {@inheritdoc}
   */
  public function getWrappingElement() {
    return $this->type == 'image' ? 'svg' : 'i';
  }

  /**
   * {@inheritdoc}
   */
  public function getChildren() {
    $build = [];
    if ($this->type == 'font') {

      // Font glyphs cannot have more than one color by default. Using CSS,
      // IcoMoon layers multiple glyphs on top of each other to implement
      // multicolor glyphs. As a result, these glyphs take more than one
      // character code and cannot have ligatures. To avoid multicolor glyphs,
      // reimport your SVG after changing all its colors to the same color.
      if (!empty($this->data['properties']['codes']) && count($this->data['properties']['codes'])) {
        for ($i = 1; $i <= count($this->data['properties']['codes']); $i++) {
          $build[]['#markup'] = '<span class="path' . $i . '"></span>';
        }
      }
    }
    if ($this->type == 'image') {
      $build['#markup'] = Markup::create('<use xlink:href="' . $this->data['directory'] . '/symbol-defs.svg#' . $this
        ->getSelector() . '"></use>');
      $build['#allowed_tags'] = [
        'use',
        'xlink',
      ];
    }
    return $build;
  }

  /**
   * {@inheritdoc}
   */
  public function addClass($classes) {
    $this->attributes
      ->addClass($classes);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function setAttributes(array $attributes) {
    $this->attributes = new Attribute($attributes);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function setAttribute($attribute, $value) {
    $this->attributes
      ->setAttribute($attribute, $value);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function toRenderable() {
    return [
      '#theme' => 'micon_icon',
      '#icon' => $this,
      '#attributes' => $this->attributes
        ->toArray(),
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function toMarkup() {
    $elements = $this
      ->toRenderable();
    return \Drupal::service('renderer')
      ->render($elements);
  }

  /**
   * {@inheritdoc}
   */
  public function toJson() {
    return json_encode(trim(preg_replace('/<!--(.|\\s)*?-->/', '', $this
      ->toMarkup()
      ->jsonSerialize())));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MiconIcon::$attributes protected property The Attribute object.
MiconIcon::$data protected property The Micon icon data.
MiconIcon::$type protected property The Micon type. Either 'font' or 'image'.
MiconIcon::addClass public function Adds classes or merges them on to array of existing CSS classes. Overrides MiconIconInterface::addClass
MiconIcon::getChildren public function Get the content entered within the icon tags. Overrides MiconIconInterface::getChildren
MiconIcon::getHex public function Get the HEX used within sudo-elements of CSS. Overrides MiconIconInterface::getHex
MiconIcon::getPackageId public function Get the id of the Micon package. Overrides MiconIconInterface::getPackageId
MiconIcon::getPackageLabel public function Get the label of the Micon package. Overrides MiconIconInterface::getPackageLabel
MiconIcon::getPrefix public function Get the unique prefix of the IcoMoon package. Overrides MiconIconInterface::getPrefix
MiconIcon::getSelector public function Get the unique selector of the IcoMoon icon. Overrides MiconIconInterface::getSelector
MiconIcon::getTag public function Get the unique tag of the IcoMoon icon. Overrides MiconIconInterface::getTag
MiconIcon::getType public function Get the type of the IcoMoon package. Overrides MiconIconInterface::getType
MiconIcon::getWrappingElement public function Get the wrapping element HTML element type. Overrides MiconIconInterface::getWrappingElement
MiconIcon::setAttribute public function Sets values for an attribute key. Overrides MiconIconInterface::setAttribute
MiconIcon::setAttributes public function Sets attributes. Overrides MiconIconInterface::setAttributes
MiconIcon::toJson public function Returns a trimmed, json encoded string of the rendered markup. Overrides MiconIconInterface::toJson
MiconIcon::toMarkup public function Returns a fully rendered Markup representation of the object. Overrides MiconIconInterface::toMarkup
MiconIcon::toRenderable public function Returns a render array representation of the object. Overrides MiconIconInterface::toRenderable
MiconIcon::__construct public function Constructs a new MiconIcon.