You are here

trait MiconIconizeTrait in Micon 8

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

Wrapper methods for \Drupal\micon\MiconIconize.

Hierarchy

2 files declare their use of MiconIconizeTrait
MiconLinkFormatter.php in micon_link/src/Plugin/Field/FieldFormatter/MiconLinkFormatter.php
MiconLinkWidget.php in micon_link/src/Plugin/Field/FieldWidget/MiconLinkWidget.php

File

src/MiconIconizeTrait.php, line 10

Namespace

Drupal\micon
View source
trait MiconIconizeTrait {

  /**
   * Transforms a string into an icon + string.
   *
   * This can be used interchangeably with the
   * \Drupal\Core\StringTranslation\StringTranslationTrait.
   *
   * @param string $string
   *   A string containing the English text to translate.
   * @param array $args
   *   (optional) An associative array of replacements to make after
   *   translation. Based on the first character of the key, the value is
   *   escaped and/or themed. See
   *   \Drupal\Component\Render\FormattableMarkup::placeholderFormat() for
   *   details.
   * @param array $options
   *   (optional) An associative array of additional options, with the following
   *   elements:
   *   - 'langcode' (defaults to the current language): A language code, to
   *     translate to a language other than what is used to display the page.
   *   - 'context' (defaults to the empty context): The context the source
   *     string belongs to.
   *
   * @return \Drupal\Core\Render\Markup
   *   An object that, when cast to a string, returns the icon markup and
   *   translated string.
   *
   * @see \Drupal\Core\StringTranslation\StringTranslationTrait::t()
   *
   * @ingroup sanitization
   */
  protected function micon($string, array $args = array(), array $options = array()) {
    return new MiconIconize($string, $args, $options, $this
      ->getStringTranslation());
  }

  /**
   * Gets the string translation service.
   *
   * @return \Drupal\Core\StringTranslation\TranslationInterface
   *   The string translation service.
   */
  protected function getMiconStringTranslation() {
    if (!$this->stringTranslation) {
      $this->stringTranslation = \Drupal::service('string_translation');
    }
    return $this->stringTranslation;
  }

  /**
   * Sets the string translation service to use.
   *
   * @param \Drupal\Core\StringTranslation\TranslationInterface $translation
   *   The string translation service.
   *
   * @return $this
   */
  public function setMiconStringTranslation(TranslationInterface $translation) {
    $this->stringTranslation = $translation;
    return $this;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MiconIconizeTrait::getMiconStringTranslation protected function Gets the string translation service.
MiconIconizeTrait::micon protected function Transforms a string into an icon + string.
MiconIconizeTrait::setMiconStringTranslation public function Sets the string translation service to use.