You are here

class Micon in Micon 2.x

Same name in this branch
  1. 2.x src/TwigExtension/Micon.php \Drupal\micon\TwigExtension\Micon
  2. 2.x src/Element/Micon.php \Drupal\micon\Element\Micon
  3. 2.x src/Entity/Micon.php \Drupal\micon\Entity\Micon
  4. 2.x src/Plugin/SocialMediaLinks/Iconset/Micon.php \Drupal\micon\Plugin\SocialMediaLinks\Iconset\Micon
Same name and namespace in other branches
  1. 8 src/Element/Micon.php \Drupal\micon\Element\Micon

Provides a one-line text field form element.

Properties:

  • #maxlength: Maximum number of characters of input allowed.

Usage example:

$form['icon'] = array(
  '#type' => 'micon',
  '#title' => $this
    ->t('Subject'),
  '#default_value' => $icon_id,
  '#required' => TRUE,
  '#packages' => [
    'fa',
  ],
);

Plugin annotation

@FormElement("micon");

Hierarchy

Expanded class hierarchy of Micon

8 string references to 'Micon'
micon.info.yml in ./micon.info.yml
micon.info.yml
micon.links.menu.yml in ./micon.links.menu.yml
micon.links.menu.yml
micon_content_type.info.yml in micon_content_type/micon_content_type.info.yml
micon_content_type/micon_content_type.info.yml
micon_link.info.yml in micon_link/micon_link.info.yml
micon_link/micon_link.info.yml
micon_local_task.info.yml in micon_local_task/micon_local_task.info.yml
micon_local_task/micon_local_task.info.yml

... See full list

10 #type uses of Micon
FileMiconFormatter::settingsForm in src/Plugin/Field/FieldFormatter/FileMiconFormatter.php
Returns a form to configure settings for the formatter.
MiconLinkFormatter::settingsForm in micon_link/src/Plugin/Field/FieldFormatter/MiconLinkFormatter.php
Returns a form to configure settings for the formatter.
MiconLinkWidget::formElement in micon_link/src/Plugin/Field/FieldWidget/MiconLinkWidget.php
Returns the form for a single field widget.
MiconLinkWidget::settingsForm in micon_link/src/Plugin/Field/FieldWidget/MiconLinkWidget.php
Returns a form to configure settings for the widget.
micon_content_type_form_node_type_form_alter in micon_content_type/micon_content_type.module
Implements hook_form_FORM_ID_alter().

... See full list

File

src/Element/Micon.php, line 29

Namespace

Drupal\micon\Element
View source
class Micon extends FormElement {

  /**
   * {@inheritdoc}
   */
  public function getInfo() {
    $class = get_class($this);
    return [
      '#input' => TRUE,
      '#process' => [
        [
          $class,
          'processMicon',
        ],
        [
          $class,
          'processAjaxForm',
        ],
      ],
      '#pre_render' => [
        [
          $class,
          'preRenderMicon',
        ],
      ],
      '#theme' => 'select',
      '#theme_wrappers' => [
        'form_element',
      ],
      '#multiple' => FALSE,
      '#packages' => [],
    ];
  }

  /**
   * Processes an Micon icon form element.
   *
   * This process callback is mandatory for select fields, since all user agents
   * automatically preselect the first available option of single (non-multiple)
   * select lists.
   *
   * @param array $element
   *   The form element to process.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   * @param array $complete_form
   *   The complete form structure.
   *
   * @return array
   *   The processed element.
   *
   * @see _form_validate()
   */
  public static function processMicon(array &$element, FormStateInterface $form_state, array &$complete_form) {

    // For proper validation we need to override the type as a select field.
    $element['#type'] = 'select';
    $element['#attributes']['class'][] = 'js-hide';
    $element['#options'] = [];

    // If the element is set to #required through #states, override the
    // element's #required setting.
    $required = isset($element['#states']['required']) ? TRUE : $element['#required'];

    // If the element is required and there is no #default_value, then add an
    // empty option that will fail validation, so that the user is required to
    // make a choice. Also, if there's a value for #empty_value or
    // #empty_option, then add an option that represents emptiness.
    if ($required && !isset($element['#default_value']) || isset($element['#empty_value']) || isset($element['#empty_option'])) {
      $element += [
        '#empty_value' => '',
        '#empty_option' => $required ? t('- Select -') : t('- None -'),
      ];

      // The empty option is prepended to #options and purposively not merged
      // to prevent another option in #options mistakenly using the same value
      // as #empty_value.
      $empty_option = [
        $element['#empty_value'] => $element['#empty_option'],
      ];
      $element['#options'] = $empty_option + $element['#options'];
    }
    else {
      $element['#options'][''] = t('- None -');
    }

    // Add icon packages as options.
    $packages = \Drupal::service('micon.icon.manager')
      ->getIcons();
    $include = is_array($element['#packages']) ? array_filter($element['#packages']) : [];
    if (!empty($include)) {
      $packages = array_intersect_key($packages, $include);
    }
    foreach ($packages as $icons) {
      foreach ($icons as $icon) {
        if (count($packages) > 1) {
          $element['#options'][$icon
            ->getPackageLabel()][$icon
            ->getSelector()] = $icon
            ->toJson();
        }
        else {
          $element['#options'][$icon
            ->getSelector()] = $icon
            ->toJson();
        }
      }
    }
    $element['#attached']['library'][] = 'micon/micon.element';
    return $element;
  }

  /**
   * {@inheritdoc}
   */
  public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
    if ($input !== FALSE) {
      if (isset($element['#empty_value']) && $input === (string) $element['#empty_value']) {
        return $element['#empty_value'];
      }
      else {
        return $input;
      }
    }
  }

  /**
   * Prepares a select render element.
   */
  public static function preRenderMicon($element) {
    Element::setAttributes($element, [
      'id',
      'name',
      'size',
    ]);
    static::setAttributes($element, [
      'form-micon',
    ]);
    return $element;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
FormElement::processAutocomplete public static function Adds autocomplete functionality to elements.
FormElement::processPattern public static function #process callback for #pattern form element property.
FormElement::validatePattern public static function #element_validate callback for #pattern form element property.
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
Micon::getInfo public function Returns the element properties for this element. Overrides ElementInterface::getInfo
Micon::preRenderMicon public static function Prepares a select render element.
Micon::processMicon public static function Processes an Micon icon form element.
Micon::valueCallback public static function Determines how user input is mapped to an element's #value property. Overrides FormElement::valueCallback
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 2
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.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. 98
RenderElement::preRenderAjaxForm public static function Adds Ajax information about an element to communicate with JavaScript.
RenderElement::preRenderGroup public static function Adds members of this group as actual elements for rendering.
RenderElement::processAjaxForm public static function Form element processing handler for the #ajax form property. 1
RenderElement::processGroup public static function Arranges elements into groups.
RenderElement::setAttributes public static function Sets a form element's class attribute. Overrides ElementInterface::setAttributes
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
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.