View source
<?php
namespace Drupal\rng;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\rng\Entity\RegistrantInterface;
use Drupal\user\Entity\User;
class RegistrantsElementUtility {
protected $element;
protected $formState;
public function __construct(array &$element, FormStateInterface &$form_state) {
$this->element = $element;
$this->formState = $form_state;
}
public static function findElement(array $form, FormStateInterface $form_state) {
return static::findElementWithProperties($form, $form_state, [
'#identity_element_root' => TRUE,
]);
}
protected static function findElementWithProperties(array $form, FormStateInterface $form_state, array $element_properties) {
$trigger = $form_state
->getTriggeringElement();
$parents = $trigger['#parents'];
$element = $form;
while (!isset($element) || array_diff_assoc($element_properties, $element)) {
$element = NestedArray::getValue($form, $parents);
if (array_pop($parents) === NULL) {
return NULL;
}
}
return $element;
}
public function addRegistrant(RegistrantInterface $registrant) {
$registrants = $this
->getRegistrants();
$registrants[] = $registrant;
$this
->setRegistrants($registrants);
}
public function replaceFirstRegistrant(RegistrantInterface $registrant) {
$registrants = $this
->getRegistrants();
$key = key($registrants);
$key = $key !== NULL ? $key : 0;
$registrants[$key] = $registrant;
$this
->setRegistrants($registrants);
}
public function getRegistrants() {
return $this->formState
->get(array_merge($this->element['#parents'], [
'registrants',
])) ?: $this->element['#value'];
}
public function setRegistrants(array $registrants) {
$this->formState
->set(array_merge($this->element['#parents'], [
'registrants',
]), $registrants);
}
public function getWhitelistExisting() {
return $this->formState
->get(array_merge($this->element['#parents'], [
'whitelist_existing',
])) ?: [];
}
public function addWhitelistExisting(EntityInterface $identity) {
$whitelisted = $this
->getWhitelistExisting();
$whitelisted[$identity
->getEntityTypeId()][$identity
->id()] = $identity
->id();
$this->formState
->set(array_merge($this->element['#parents'], [
'whitelist_existing',
]), $whitelisted);
}
public function getChangeIt() {
return $this->formState
->get(array_merge($this->element['#parents'], [
'change_it',
])) ?: FALSE;
}
public function setChangeIt($value) {
$this->formState
->set(array_merge($this->element['#parents'], [
'change_it',
]), $value);
}
public function getShowCreateEntitySubform() {
return (bool) $this->formState
->get(array_merge($this->element['#parents'], [
'show_entity_create_form',
]));
}
public function setShowCreateEntitySubform($value) {
$this->formState
->set(array_merge($this->element['#parents'], [
'show_entity_create_form',
]), $value);
}
public function clearPeopleFormInput() {
$autocomplete_tree = array_merge($this->element['#parents'], [
'entities',
'person',
'existing',
'existing_autocomplete',
]);
NestedArray::unsetValue($this->formState
->getUserInput(), $autocomplete_tree);
$registrant_tree = array_merge($this->element['#parents'], [
'entities',
'person',
'registrant',
]);
NestedArray::unsetValue($this->formState
->getUserInput(), $registrant_tree);
$new_entity_tree = array_merge($this->element['#parents'], [
'entities',
'person',
'new_person',
'newentityform',
]);
NestedArray::unsetValue($this->formState
->getUserInput(), $new_entity_tree);
}
public function buildRegistrant($validate = FALSE) {
$value = $this->formState
->getTemporaryValue(array_merge([
'_registrants_values',
], $this->element['#parents']));
$this->formState
->setValue($this->element['#parents'], $value);
$registrant = $this->formState
->get('registrant__entity');
$display = $this->formState
->get('registrant__form_display');
$registrant_tree = [
'entities',
'person',
'registrant',
];
$subform_registrant = NestedArray::getValue($this->element, $registrant_tree);
$display
->extractFormValues($registrant, $subform_registrant, $this->formState);
if ($validate) {
$display
->validateFormValues($registrant, $subform_registrant, $this->formState);
}
return $registrant;
}
public function setForBundleAsFirstRegistrant() {
$registrants = $this->element['#value'];
$registrant = reset($registrants);
if ($registrant) {
$identity = $registrant
->getIdentity();
$entity_type = $identity
->getEntityTypeId();
$bundle = $identity
->bundle();
$new_value = "{$entity_type}:{$bundle}";
$for_bundle_tree = array_merge($this->element['#parents'], [
'entities',
'for_bundle',
]);
NestedArray::setValue($this->formState
->getUserInput(), $for_bundle_tree, $new_value);
}
}
public function identityExists(EntityInterface $identity) {
$registrants = $this
->getRegistrants();
foreach ($registrants as $registrant) {
if ($registrant
->getIdentity() && $registrant
->getIdentity()
->id() == $identity
->id() && $registrant
->getIdentity()
->getEntityTypeId() == $identity
->getEntityTypeId()) {
return TRUE;
}
}
return FALSE;
}
public function countReferenceableEntities(EntityInterface $event, $entity_type_id, $bundles = []) {
$selection_manager = \Drupal::service('plugin.manager.entity_reference_selection');
$options = [
'target_type' => $entity_type_id,
'handler' => 'rng_register',
'handler_settings' => [
'event_entity_type' => $event
->getEntityTypeId(),
'event_entity_id' => $event
->id(),
],
];
if (!empty($bundles)) {
$options['handler_settings']['target_bundles'] = $bundles;
}
$selection = $selection_manager
->getInstance($options);
return $selection
->countReferenceableEntities();
}
public function peopleTypeOptions() {
$event_manager = \Drupal::service('rng.event_manager');
$bundle_info = \Drupal::service('entity_type.bundle.info');
$entity_type_manager = \Drupal::entityTypeManager();
$current_user = \Drupal::currentUser();
$event = $this->element['#event'];
$event_meta = $event_manager
->getMeta($event);
$event_type = $event_meta
->getEventType();
$for_bundles = [];
if ($event_type
->getAllowAnonRegistrants()) {
$for_bundles['anon:'] = t('Registrant');
}
foreach ($this->element['#allow_creation'] as $entity_type_id => $bundles) {
$info = $bundle_info
->getBundleInfo($entity_type_id);
foreach ($bundles as $bundle) {
if ($this
->entityCreateAccess($entity_type_id, $bundle)) {
$for_bundle_key = $entity_type_id . ':' . $bundle;
$for_bundles[$for_bundle_key] = $info[$bundle]['label'];
}
}
}
foreach ($this->element['#allow_reference'] as $entity_type_id => $bundles) {
$entity_type = $entity_type_manager
->getDefinition($entity_type_id);
$info = $bundle_info
->getBundleInfo($entity_type_id);
foreach ($bundles as $bundle) {
$for_bundle_key = $entity_type_id . ':' . $bundle;
if (isset($for_bundles[$for_bundle_key])) {
continue;
}
$counting_bundle = $entity_type
->getBundleEntityType() !== NULL ? [
$bundle,
] : [];
$existing_count = $this
->countReferenceableEntities($event, $entity_type_id, $counting_bundle);
if ($entity_type_id === 'user' && $current_user
->isAuthenticated()) {
$identity = User::load($current_user
->id());
if (!$this
->identityExists($identity)) {
if ($event_meta
->identitiesCanRegister('user', [
$current_user
->id(),
])) {
$existing_count--;
$for_bundles['myself:'] = t('Myself');
}
}
}
if ($existing_count > 0) {
$for_bundles[$for_bundle_key] = $info[$bundle]['label'];
}
}
}
return $for_bundles;
}
public static function entityCreateAccess($entity_type_id, $bundle) {
$entity_type_manager = \Drupal::entityTypeManager();
$entity_type = $entity_type_manager
->getDefinition($entity_type_id);
$access_control = $entity_type_manager
->getAccessControlHandler($entity_type_id);
$entity_bundle = $entity_type
->getBundleEntityType() !== NULL ? $bundle : NULL;
return $access_control
->createAccess($entity_bundle);
}
}