GroupSelectorWidget.php in Opigno calendar event 3.x
File
modules/opigno_calendar_event_group/src/GroupSelectorWidget.php
View source
<?php
namespace Drupal\opigno_calendar_event_group;
use Drupal\opigno_calendar_event\CalendarEventExceptionLoggerTrait;
use Drupal\opigno_calendar_event\CalendarEventInterface;
use Drupal\opigno_calendar_event\CalendarEventManager;
use Drupal\opigno_calendar_event\Form\CalendarEventEmbeddedWidget;
use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\group\Entity\GroupInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class GroupSelectorWidget implements ContainerInjectionInterface {
use StringTranslationTrait;
use CalendarEventExceptionLoggerTrait;
protected $entityTypeManager;
protected $calendarEventManager;
public function __construct(EntityTypeManagerInterface $entity_type_manager, CalendarEventManager $calendar_event_manager) {
$this->entityTypeManager = $entity_type_manager;
$this->calendarEventManager = $calendar_event_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'), $container
->get('opigno_calendar_event.manager'));
}
public function alterForm(array &$form, FormStateInterface $form_state) {
$form_object = $form_state
->getFormObject();
$entity = $form_object
->getEntity();
$groups = [];
if ($entity
->isNew()) {
$group = $form_state
->get('group');
if ($group instanceof GroupInterface) {
$groups[$group
->id()] = $group;
}
}
else {
$groups = $this
->getSelectableGroups($entity);
}
if (!$groups) {
return;
}
$elements =& $form[CalendarEventEmbeddedWidget::ELEMENT_NAME];
foreach (Element::children($elements) as $delta) {
if (is_int($delta)) {
$this
->addGroupPicker($elements[$delta], $groups, $form, $form_state);
}
}
}
protected function addGroupPicker(array &$element, array $groups, array $form, FormStateInterface $form_state) {
if (!isset($element['displayed'])) {
return;
}
$options = array_map(function (GroupInterface $group) {
return $group
->label();
}, $groups);
$options['_displayed'] = $this
->t('Global calendar');
$widget = $this->calendarEventManager
->getEmbeddedWidget();
$state = $widget
->getItemState($form, $form_state, $element['#delta']);
$calendar_event = $state['entity'];
$default_values = array_map(function ($item) {
return $item['target_id'];
}, $calendar_event
->get('display_groups')
->getValue());
$widget_element =& $element['displayed']['widget']['value'];
if (!empty($widget_element['#default_value'])) {
$default_values[] = '_displayed';
}
$widget_element = [
'#type' => 'checkboxes',
'#options' => $options,
'#default_value' => $default_values,
] + $widget_element;
$element['#entity_builders'][] = [
$this,
'buildEntity',
];
}
protected function getSelectableGroups(ContentEntityInterface $entity) {
$groups = [];
try {
$storage = $this->entityTypeManager
->getStorage('group_content');
foreach ($storage
->loadByEntity($entity) as $group_content) {
$group = $group_content
->getGroup();
$groups[$group
->id()] = $group;
}
} catch (InvalidPluginDefinitionException $e) {
$this
->logException($e);
}
return $groups;
}
public function buildEntity($entity_type_id, CalendarEventInterface $calendar_event, array &$element, FormStateInterface $form_state) {
$widget = $this->calendarEventManager
->getEmbeddedWidget();
$values = $widget
->getItemValues($form_state
->getCompleteForm(), $form_state, $element['#delta']);
if (isset($values['displayed']['value']['_displayed'])) {
$calendar_event
->setDisplayed($values['displayed']['value']['_displayed'] !== 0);
unset($values['displayed']['value']['_displayed']);
}
if (!empty($values['displayed']['value'])) {
$items = array_filter($values['displayed']['value']);
$calendar_event
->set('display_groups', $items);
}
}
}