ClassUtilsTrait.php in Scheduled Updates 8
File
src/ClassUtilsTrait.php
View source
<?php
namespace Drupal\scheduled_updates;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\node\NodeInterface;
use Drupal\user\EntityOwnerInterface;
trait ClassUtilsTrait {
protected function implementsInterface($toCheck, array $interfaces) {
if (empty($toCheck)) {
return FALSE;
}
if (array_intersect($interfaces, class_implements($toCheck))) {
return TRUE;
}
return FALSE;
}
protected function revisionOwnerInterfaces() {
return [
'Drupal\\entity\\Revision\\EntityRevisionLogInterface',
'Drupal\\node\\NodeInterface',
];
}
protected function getRevisionOwner(ContentEntityInterface $entity) {
if ($entity instanceof NodeInterface) {
return $entity
->getRevisionAuthor();
}
elseif ($this
->implementsInterface($entity, [
'Drupal\\entity\\Revision\\EntityRevisionLogInterface',
])) {
return $entity
->getRevisionUser();
}
return NULL;
}
protected function getEntityOwner(ContentEntityInterface $entity) {
if ($entity instanceof EntityOwnerInterface) {
return $entity
->getOwner();
}
return NULL;
}
protected function entityTypeManager() {
if (isset($this->entityTypeManager)) {
return $this->entityTypeManager;
}
return NULL;
}
protected function entityLabel($type_id) {
return $this
->entityTypeManager()
->getDefinition($type_id)
->getLabel();
}
protected function bundleLabel($type_id) {
return $this
->entityTypeManager()
->getDefinition($type_id)
->getBundleLabel();
}
protected function typeSupportsBundles($type_id) {
$bundle_type = $this
->entityTypeManager()
->getDefinition($type_id)
->getBundleEntityType();
if (empty($bundle_type)) {
return FALSE;
}
return TRUE;
}
public function targetTypeLabel(ScheduledUpdateTypeInterface $scheduledUpdateType) {
return $this
->entityLabel($scheduledUpdateType
->getUpdateEntityType());
}
protected function targetTypeBundleLabel(ScheduledUpdateTypeInterface $scheduledUpdateType) {
return $this
->bundleLabel($scheduledUpdateType
->getUpdateEntityType());
}
protected function targetSupportBundles(ScheduledUpdateTypeInterface $scheduledUpdateType) {
return $this
->typeSupportsBundles($scheduledUpdateType
->getUpdateEntityType());
}
protected function definitionClassImplementsInterface(EntityTypeInterface $type, array $interfaces) {
return $this
->implementsInterface($type
->getClass(), $interfaces);
}
}