Make entity query bundle optional and entity type default to node
This commit is contained in:
parent
097611f89a
commit
e845aa76aa
1 changed files with 11 additions and 4 deletions
|
@ -3,18 +3,25 @@
|
|||
/**
|
||||
* Entity query helper.
|
||||
*/
|
||||
function migration_helpers_entity_query($entity_type, $bundle, $field = '') {
|
||||
function migration_helpers_entity_query(
|
||||
$entity_type = 'node',
|
||||
$bundle = NULL,
|
||||
$field = NULL,
|
||||
) {
|
||||
|
||||
/** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager */
|
||||
$entity_type_manager = \Drupal::service('entity_type.manager');
|
||||
|
||||
$entity_storage = $entity_type_manager->getStorage($entity_type);
|
||||
$bundle_key = $entity_storage->getEntityType()->getKey('bundle');
|
||||
|
||||
$entity_query = $entity_storage->getQuery();
|
||||
$entity_query->condition($bundle_key, $bundle);
|
||||
|
||||
if (!empty($field)) {
|
||||
if ($bundle) {
|
||||
$bundle_key = $entity_storage->getEntityType()->getKey('bundle');
|
||||
$entity_query->condition($bundle_key, $bundle);
|
||||
}
|
||||
|
||||
if ($field) {
|
||||
// Check that field BOTH is present on entity bundle AND has a value saved.
|
||||
$entity_query->exists($field);
|
||||
}
|
||||
|
|
Reference in a new issue