MiconMenuWidget.php in Micon 2.x
File
micon_menu/src/Plugin/Field/FieldWidget/MiconMenuWidget.php
View source
<?php
namespace Drupal\micon_menu\Plugin\Field\FieldWidget;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\micon_link\Plugin\Field\FieldWidget\MiconLinkWidget;
class MiconMenuWidget extends MiconLinkWidget {
protected function getPackages() {
$config = \Drupal::config('micon_menu.config');
return $config
->get('packages');
}
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$element = parent::formElement($items, $delta, $element, $form, $form_state);
$element['options']['attributes']['data-icon']['#access'] = \Drupal::currentUser()
->hasPermission('use micon menu');
return $element;
}
public static function validateIconElement($element, FormStateInterface $form_state, $form) {
parent::validateIconElement($element, $form_state, $form);
if ($values = $form_state
->getValue('link')) {
foreach ($values as $value) {
if ($attributes = $form_state
->getValue('attributes')) {
if (!empty($value['options']['attributes'])) {
$attributes += $value['options']['attributes'];
$form_state
->setValue('attributes', $attributes);
}
}
}
}
}
public static function isApplicable(FieldDefinitionInterface $field_definition) {
return $field_definition
->getTargetEntityTypeId() == 'menu_link_content';
}
}