You are here

class MiconIconize in Micon 2.x

Same name and namespace in other branches
  1. 8 src/MiconIconize.php \Drupal\micon\MiconIconize

Class MiconIconize.

@package Drupal\micon

Hierarchy

Expanded class hierarchy of MiconIconize

3 files declare their use of MiconIconize
EntityReferenceMiconFormatter.php in src/Plugin/Field/FieldFormatter/EntityReferenceMiconFormatter.php
micon.module in ./micon.module
Contains micon.module.
micon_menu.module in micon_menu/micon_menu.module
Contains micon_menu.module.

File

src/MiconIconize.php, line 14

Namespace

Drupal\micon
View source
class MiconIconize extends TranslatableMarkup {

  /**
   * The Micon management service.
   *
   * @var \Drupal\micon\MiconManager
   */
  protected $miconManager;

  /**
   * The Micon icon management service.
   *
   * @var \Drupal\micon\MiconIconManager
   */
  protected $miconDiscoveryManager;

  /**
   * The renderer.
   *
   * @var \Drupal\Core\Render\RendererInterface
   */
  protected $renderer;

  /**
   * The MiconIcon object.
   *
   * @var \Drupal\micon\MiconIcon|null
   */
  protected $icon;

  /**
   * The string to match within icon definitions.
   *
   * @var string
   */
  protected $matchString;

  /**
   * The match prefix to append to the match string.
   *
   * @var array
   */
  protected $matchPrefix = [
    '',
  ];

  /**
   * The Icon display options.
   *
   * @var array
   */
  protected $display = [
    'iconOnly' => FALSE,
    'iconPosition' => 'before',
  ];

  /**
   * {@inheritdoc}
   */
  public function __construct($string = '', array $arguments = [], array $options = [], TranslationInterface $string_translation = NULL) {
    if (is_a($string, '\\Drupal\\Core\\StringTranslation\\TranslatableMarkup')) {
      $arguments = $string
        ->getArguments();
      $options = $string
        ->getOptions();
      $string_translation = $string
        ->getStringTranslation();
      $string = $string
        ->getUntranslatedString();
    }
    parent::__construct($string, $arguments, $options, $string_translation);
    $this->miconManager = \Drupal::service('micon.icon.manager');
    $this->miconDiscoveryManager = \Drupal::service('plugin.manager.micon.discovery');
    $this->renderer = \Drupal::service('renderer');
  }

  /**
   * Return a class instance.
   */
  public static function iconize($string = '', array $arguments = [], array $options = [], TranslationInterface $string_translation = NULL) {
    return new static($string, $arguments, $options, $string_translation);
  }

  /**
   * {@inheritdoc}
   */
  public function render() {
    $return = $this
      ->getTitle();
    $icon = $this
      ->getIcon();
    if ($icon) {
      if (!$return) {
        $output = [
          '#theme' => 'micon_icon',
          '#icon' => $icon,
        ];
      }
      else {
        $output = [
          '#theme' => 'micon',
          '#icon' => $icon,
          '#title' => Markup::create($return),
          '#icon_only' => $this->display['iconOnly'],
          '#position' => $this->display['iconPosition'],
        ];
      }
      return $this->renderer
        ->render($output);
    }
    return $return;
  }

  /**
   * Only show the icon.
   *
   * @param bool $iconOnly
   *   (optional) Whether to hide the string and only show the icon.
   *
   * @return $this
   */
  public function setIconOnly($iconOnly = TRUE) {
    $this->display['iconOnly'] = $iconOnly;
    return $this;
  }

  /**
   * Show the icon before the title.
   *
   * @return $this
   */
  public function setIconBefore() {
    $this->display['iconPosition'] = 'before';
    return $this;
  }

  /**
   * Show the icon before the title.
   *
   * @return $this
   */
  public function setIconAfter() {
    $this->display['iconPosition'] = 'after';
    return $this;
  }

  /**
   * Given a string, check to see if we have an Micon package icon match.
   *
   * If found it will be set it as the current icon. Using this method to set
   * the icon will skip any automatic text icon lookup.
   *
   * @param string $icon_id
   *   The ID if the icon that should be used. This ID is defined in the
   *   Micon package.
   *
   * @return $this
   */
  public function setIcon($icon_id) {
    $this->icon = $this->miconManager
      ->getIconMatch($icon_id);
    return $this;
  }

  /**
   * Given a string, return the MiconIcon match.
   *
   * If an icon has been found and set using setIcon() that icon will be
   * immediately returned.
   *
   * @param bool $force_match
   *   Force a match lookup even if $this->icon is already set.
   *
   * @return \Drupal\micon\MiconIcon|null
   *   The MiconIcon if found, else null.
   */
  public function getIcon($force_match = FALSE) {
    if ($force_match || !$this->icon) {
      $this
        ->getMatch($this
        ->getMatchString());
    }
    return $this->icon;
  }

  /**
   * Render the object as the title only.
   *
   * @return string
   *   The translated string.
   */
  public function getTitle() {
    return parent::render();
  }

  /**
   * Match a string agaist definition and packages.
   *
   * Match a string against the icon definitions and then against the
   * Micon icon packages and return it as a MiconIcon if it exists.
   *
   * @param string $string
   *   A string that will be used to search through the icon definitions as well
   *   as the Micon icons to return a confirmed match.
   *
   * @return \Drupal\micon\MiconIcon|null
   *   The MiconIcon if found, else null.
   */
  public function getMatch($string) {
    foreach ($this
      ->getMatchPrefix() as $prefix) {
      if ($icon_id = $this->miconDiscoveryManager
        ->getDefinitionMatch($prefix . $string)) {
        $this
          ->setIcon($icon_id);
        break;
      }
    }
    return $this->icon;
  }

  /**
   * The prefix that will be appended to the match string.
   *
   * @param string $string
   *   A string that will be appended to the match string. This is useful when
   *   you want to provide icons with more specific replacements.
   *
   * @return $this
   */
  public function addMatchPrefix($string) {
    $this->matchPrefix[] = $string . '.';
    return $this;
  }

  /**
   * Return the match prefix.
   *
   * @return string
   *   The match prefix.
   */
  public function getMatchPrefix() {
    return $this->matchPrefix;
  }

  /**
   * The machine string to use as the match when looking for icons.
   *
   * @param string $string
   *   A string that will be used to search through the icon definitions as well
   *   as the Micon icons to return a confirmed match.
   *
   * @return $this
   */
  public function setMatchString($string) {
    if (is_a($string, '\\Drupal\\Core\\StringTranslation\\TranslatableMarkup')) {
      $string = $string
        ->getUntranslatedString();
    }
    $this->matchString = strtolower(strip_tags($string));
    return $this;
  }

  /**
   * Return cleaned and lowercase string.
   */
  protected function getMatchString() {
    if (!isset($this->matchString)) {
      $this
        ->setMatchString($this
        ->getUntranslatedString());
    }
    return $this->matchString;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FormattableMarkup::$arguments protected property The arguments to replace placeholders with.
FormattableMarkup::$string protected property The string containing placeholders.
FormattableMarkup::jsonSerialize public function Returns a representation of the object for use in JSON serialization.
FormattableMarkup::placeholderEscape protected static function Escapes a placeholder replacement value if needed.
FormattableMarkup::placeholderFormat protected static function Replaces placeholders in a string with values.
MiconIconize::$display protected property The Icon display options.
MiconIconize::$icon protected property The MiconIcon object.
MiconIconize::$matchPrefix protected property The match prefix to append to the match string.
MiconIconize::$matchString protected property The string to match within icon definitions.
MiconIconize::$miconDiscoveryManager protected property The Micon icon management service.
MiconIconize::$miconManager protected property The Micon management service.
MiconIconize::$renderer protected property The renderer.
MiconIconize::addMatchPrefix public function The prefix that will be appended to the match string.
MiconIconize::getIcon public function Given a string, return the MiconIcon match.
MiconIconize::getMatch public function Match a string agaist definition and packages.
MiconIconize::getMatchPrefix public function Return the match prefix.
MiconIconize::getMatchString protected function Return cleaned and lowercase string.
MiconIconize::getTitle public function Render the object as the title only.
MiconIconize::iconize public static function Return a class instance.
MiconIconize::render public function Renders the object as a string. Overrides TranslatableMarkup::render
MiconIconize::setIcon public function Given a string, check to see if we have an Micon package icon match.
MiconIconize::setIconAfter public function Show the icon before the title.
MiconIconize::setIconBefore public function Show the icon before the title.
MiconIconize::setIconOnly public function Only show the icon.
MiconIconize::setMatchString public function The machine string to use as the match when looking for icons.
MiconIconize::__construct public function Constructs a new class instance. Overrides TranslatableMarkup::__construct
ToStringTrait::_die protected function For test purposes, wrap die() in an overridable method.
ToStringTrait::__toString public function Implements the magic __toString() method.
TranslatableMarkup::$options protected property The translation options.
TranslatableMarkup::$stringTranslation protected property The string translation service.
TranslatableMarkup::$translatedMarkup protected property The translated markup without placeholder replacements.
TranslatableMarkup::count public function Returns the string length. Overrides FormattableMarkup::count
TranslatableMarkup::getArguments public function Gets all arguments from this translated string.
TranslatableMarkup::getOption public function Gets a specific option from this translated string.
TranslatableMarkup::getOptions public function Gets all options from this translated string.
TranslatableMarkup::getStringTranslation protected function Gets the string translation service.
TranslatableMarkup::getUntranslatedString public function Gets the untranslated string value stored in this translated string.
TranslatableMarkup::__sleep public function Magic __sleep() method to avoid serializing the string translator. 1