StatusProperties.php in Select 2 8
File
modules/select2_publish/src/Element/StatusProperties.php
View source
<?php
namespace Drupal\select2_publish\Element;
use Drupal\Core\Entity\EntityPublishedInterface;
use Drupal\Core\Security\TrustedCallbackInterface;
class StatusProperties implements TrustedCallbackInterface {
public static function trustedCallbacks() {
return [
'preRender',
];
}
public static function preRender(array $element) {
if ($element['#target_type']) {
$entity_manager = \Drupal::entityTypeManager();
$entity_definition = $entity_manager
->getDefinition($element['#target_type']);
if (!$entity_definition
->entityClassImplements(EntityPublishedInterface::class)) {
return $element;
}
$entity_storage = $entity_manager
->getStorage($element['#target_type']);
foreach ($entity_storage
->loadMultiple(array_keys($element['#options'])) as $id => $entity) {
$element['#options_attributes'][$id]['data-published'] = $entity
->isPublished() ? 'true' : 'false';
}
$default_status = 'true';
if ($element['#autocreate']) {
$entity = $entity_storage
->create([
$entity_definition
->getKey('bundle') => $element['#autocreate']['bundle'],
]);
$default_status = $entity
->isPublished() ? 'true' : 'false';
}
$element['#attached']['library'][] = 'select2_publish/select2.publish';
$element['#attributes']['data-select2-publish-default'] = $default_status;
}
return $element;
}
}