class Micon in Micon 2.x
Same name in this branch
- 2.x src/TwigExtension/Micon.php \Drupal\micon\TwigExtension\Micon
- 2.x src/Element/Micon.php \Drupal\micon\Element\Micon
- 2.x src/Entity/Micon.php \Drupal\micon\Entity\Micon
- 2.x src/Plugin/SocialMediaLinks/Iconset/Micon.php \Drupal\micon\Plugin\SocialMediaLinks\Iconset\Micon
Same name and namespace in other branches
- 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
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\Core\Render\Element\RenderElement implements ElementInterface
- class \Drupal\Core\Render\Element\FormElement implements FormElementInterface
- class \Drupal\micon\Element\Micon
- class \Drupal\Core\Render\Element\FormElement implements FormElementInterface
- class \Drupal\Core\Render\Element\RenderElement implements ElementInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
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
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().
File
- src/
Element/ Micon.php, line 29
Namespace
Drupal\micon\ElementView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
public | function | 2 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormElement:: |
public static | function | Adds autocomplete functionality to elements. | |
FormElement:: |
public static | function | #process callback for #pattern form element property. | |
FormElement:: |
public static | function | #element_validate callback for #pattern form element property. | |
MessengerTrait:: |
protected | property | The messenger. | 27 |
MessengerTrait:: |
public | function | Gets the messenger. | 27 |
MessengerTrait:: |
public | function | Sets the messenger. | |
Micon:: |
public | function |
Returns the element properties for this element. Overrides ElementInterface:: |
|
Micon:: |
public static | function | Prepares a select render element. | |
Micon:: |
public static | function | Processes an Micon icon form element. | |
Micon:: |
public static | function |
Determines how user input is mapped to an element's #value property. Overrides FormElement:: |
|
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
2 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginBase:: |
public | function | Constructs a \Drupal\Component\Plugin\PluginBase object. | 98 |
RenderElement:: |
public static | function | Adds Ajax information about an element to communicate with JavaScript. | |
RenderElement:: |
public static | function | Adds members of this group as actual elements for rendering. | |
RenderElement:: |
public static | function | Form element processing handler for the #ajax form property. | 1 |
RenderElement:: |
public static | function | Arranges elements into groups. | |
RenderElement:: |
public static | function |
Sets a form element's class attribute. Overrides ElementInterface:: |
|
StringTranslationTrait:: |
protected | property | The string translation service. | 4 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |