Usage:
Load an entity object
$user = entityManage('user', 1);
$node = entityManage('node', 1);
Update a node's title
entityManage('node', 1, ['title' => 'New title']);
Get a list of inactive users
$inactiveUsers = entityManage('user', ['status' => 0]);
Get a list of taxonomy terms
$terms = entityManage('taxonomy_term', ['vid' => 'tags']);
Update taxonomy terms that have the name 'TAG NAME'
entityManage('taxonomy_term', ['vid' => 'tags', 'name' => 'TAG NAME'], ['vid' => 'tags', 'name' => 'NEW TAG NAME']);
Delete a node
entityManage('node', 1, 'del');
Delete inactive users
entityManage('user', ['status' => 0], 'del');
Delete a taxonomy term
entityManage('taxonomy_term', ['vid' => 'tags', 'name' => 'NEW TAG NAME'], 'del');
/*
* Entity Manager - a wrapper for entity CRUD & listing operations.
* If $values is empty, it returns the request entity if found, else false.
*/
function entityManage($entity_type, $entityIdArray = [], $values = []) {
# Load all entities if no Id and no values were supplied
if (empty($entityIdArray) && empty($values))
return \Drupal::entityTypeManager()->getStorage($entity_type)->loadMultiple();
# Try loading the supplied entity
if (is_array($entityIdArray)) # Find entity using an arry of properties (e.g. vid and term name)
$entity = \Drupal::entityTypeManager()->getStorage($entity_type)->loadByProperties($entityIdArray);
else # Find entity using a non-array property (e.g. Id)
$entity = \Drupal::entityTypeManager()->getStorage($entity_type)->load($entityIdArray);
# Entity exists
if (!empty($entity)) {
# Use the entity object (not array, in case array with object is returned)
if (is_array($entity) && count($entity) == 1) $entity = $entity[key($entity)];
# Return entities (in DESC order by keys) if Id or properties were supplied but no values
if (!empty($entityIdArray) && empty($values)) {
if (is_array($entity)) { // If multiple, then order by keys DESC
krsort($entity);
return $entity;
} else
return $entity;
}
# Delete the entity if requested
if (!empty($values) && $values == 'del') {
if (is_array($entity)) { // Multiple
foreach ($entity as $e) {
$e->delete();
}
return;
} else { // One
$entity->delete();
return;
}
}
# Update the entity
foreach ($values as $k => $v) $entity->set($k, $v);
$entity->save();
} else { # Initial entity does not exist
if (!empty($values)) { # Continue if values were supplied
# Checks for created entity.
# Fixes duplication if this is run more than once when an entity property gets changed (e.g. term name change)
if (is_array($entityIdArray)) {
# Array with properties
$ak = array_keys($entityIdArray);
$newEntityIdArray = [];
# Use supplied values for entity search
if (is_array($values)) {
foreach ($ak as $k) $newEntityIdArray = array_merge($newEntityIdArray, [$k => $values[$k]]);
}
# Find entity using an arry of properties (e.g. vid and term name)
$entity = \Drupal::entityTypeManager()->getStorage($entity_type)->loadByProperties($newEntityIdArray);
} else # Find entity using a non-array property (e.g. Id)
$entity = \Drupal::entityTypeManager()->getStorage($entity_type)->load($entityIdArray);
# Entity not previously created
if (empty($entity)) {
# Create entity with supplied values
$entity = \Drupal::entityTypeManager()->getStorage($entity_type)->create($values);
# Save the entity
$entity->save();
}
# Use the entity object (in case array with object is returned)
if (is_array($entity)) $entity = $entity[key($entity)];
}
}
# Return the object of the found/created/updated entity
return !empty($entity) ? $entity : false;
}