View source
<?php
namespace Drupal\social_profile\Plugin\Field\FieldWidget;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\select2\Plugin\Field\FieldWidget\Select2EntityReferenceWidget;
use Drupal\social_profile\SocialProfileTagServiceInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class SocialProfileTagSplitWidget extends Select2EntityReferenceWidget {
protected $profileTagService;
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$widget = parent::create($container, $configuration, $plugin_id, $plugin_definition);
$widget
->setSocialProfileTagService($container
->get('social_profile.tag_service'));
return $widget;
}
protected function setSocialProfileTagService(SocialProfileTagServiceInterface $social_profile_tag_service) {
$this->profileTagService = $social_profile_tag_service;
}
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$element = parent::formElement($items, $delta, $element, $form, $form_state);
if (!$this->profileTagService
->isActive() || !$this->profileTagService
->hasContent() || empty($element['#options'])) {
return [];
}
if ($element['#target_type'] !== 'taxonomy_term') {
return $element;
}
if (!$this->profileTagService
->allowSplit()) {
$options =& $element['#options'];
$term_ids = array_keys($options);
$options = $this->profileTagService
->getTermOptionNames($term_ids);
return $element;
}
$default_value = $element['#default_value'];
$element = [
'#type' => 'details',
'#open' => TRUE,
'#element_validate' => [
[
get_class($this),
'validateElement',
],
],
'#access' => FALSE,
];
$categories = $this->profileTagService
->getCategories();
foreach ($categories as $tid => $category) {
$field_name = 'profile_tagging_' . $this->profileTagService
->tagLabelToMachineName($category);
$options = $this->profileTagService
->getChildrens($tid);
if ($this->profileTagService
->useCategoryParent()) {
$options = [
$tid => $category,
] + $options;
}
if (count($options) > 0) {
$element[$field_name] = [
'#type' => 'select2',
'#title' => $category,
'#multiple' => TRUE,
'#default_value' => $default_value,
'#options' => $options,
];
$element['#access'] = TRUE;
}
}
return $element;
}
public static function validateElement(array $element, FormStateInterface $form_state) {
$profile_tag_service = \Drupal::service('social_profile.tag_service');
if ($profile_tag_service
->allowSplit()) {
$build_info = $form_state
->getBuildInfo();
if ($build_info['form_id'] === 'user_register_form') {
$field_element = [
'entity_profile',
$element['#field_name'],
];
}
else {
$field_element = $element['#field_name'];
}
$value = $form_state
->getValue($field_element);
$field_value = [];
$categories = $profile_tag_service
->getCategories();
foreach ($categories as $tid => $category) {
$field_name = 'profile_tagging_' . $profile_tag_service
->tagLabelToMachineName($category);
if (isset($value[$field_name])) {
$field_value += $value[$field_name];
}
}
$form_state
->setValue($field_element, $field_value);
}
else {
parent::validateElement($element, $form_state);
}
}
}