Score:0

programmatically get child nodes referencing parent by entity reference uuid, using loadByProperties & entityTypeManager or other way without views

cn flag

I tried this answer:

https://drupal.stackexchange.com/a/298084/1082

But I can't seem to use

'field_scientists' => $entity_reference_uuid

where, in my case, field_scientists is a field of type Entity Reference UUID - which is a field type provided by https://www.drupal.org/project/entity_reference_uuid module

What happens for me is that I get nothing back, my results array from that example is empty.

Looking to do this programmatically in my own custom module, rather than use a Drupal View.

If there's another way e.g. that uses the ->query method for querying the database I'd be happy to use that programmatic approach.

Thank you.

Update

An example of a uuid of a node in my case is BMSSYS0000051988.

This was set in a migration import from a field in a CSV.

My attempts are:

1. based on the example in the other question: https://drupal.stackexchange.com/a/298084/1082

  $nodes = \Drupal::entityTypeManager()->getStorage('node')->loadByProperties([
    'type' => 'bol',
    'field_parent_uuid' => $taxonkey,
  ]);

2. db query

$query = \Drupal::entityQuery('node')
->condition('status', NODE_PUBLISHED)
->condition('type', 'bol');
$and = $query->andConditionGroup();
$and->condition('field_parent_uuid',  $taxonkey);
$query->condition($and);
$result = $query->execute();

where $taxonkey is BMSSYS0000051988

both 1 and 2 attempts above return an empty array.

Update 2

here is my migration config that shows uuid being written to from a field in the csv. Is this legal? Should a developer's code change the uuid value or should uuid only be handled by Drupal core?

dependencies: {  }
id: bol_csv_import
class: null
field_plugin_method: null
cck_plugin_method: null
migration_tags: null
migration_group: defaultl: publicbins
label: 'Import bol species data'
source:
  plugin: csv
  path: ./data/taxonomy_species_with_unpacked_bold.csv
  delimiter: ','
  enclosure: '"'
  header_offset: 0
  ids:
    - uuid
  fields:
    -
      name: uuidl: publicbins
      label: 'NBN id'
    -
      name: parent_uuid
      label: 'NBN id of parent'
    -
      name: name
      label: Name
    -
      name: rank
      label: 'Taxonomic Rank'
    -
      name: taxon_key
      label: 'Taxon Key'
    -
      name: authority
      label: authority
    -
      name: specimenrecords
      label: specimenrecords
    -
      name: publicrecords
      label: publicrecords
    -
      name: publicbins
      label: publicbins
    -
      name: taxid
      label: 'NBN Taxon ID'
process:
  title: name
  uid:
    plugin: default_value
    default_value: 1
  uuid: uuid
  field_parent_uuid: parent_uuid
  field_taxon_key: taxon_key
  field_authority: authority
  field_rank: rank
  field_num_bold_barcode_specimens: specimenrecords
  field_num_bold_public_records: publicrecords
  field_num_bold_public_bins: publicbins
  field_bold_taxon_id: taxid
  type:
    plugin: default_value
    default_value: bol
destination:
  plugin: 'entity:node'
migration_dependencies: null

Update 3

I'll write a db query to solve this issue. Running a command line sql indicates this approach could work:

MariaDB [db]> select * from node where nid = 246;
+-----+------+------+------------------+----------+
| nid | vid  | type | uuid             | langcode |
+-----+------+------+------------------+----------+
| 246 |  248 | bol  | NBNORG0000041189 | en       |
+-----+------+------+------------------+----------+
1 row in set (0.001 sec)
4uk4 avatar
cn flag
`BMSSYS0000051988` is not a [UUID](https://en.wikipedia.org/wiki/Universally_unique_identifier).
cn flag
+1 Thank you @4uk4 that could be helpful `uuid` was set as that in the migration. When I do `$node->uuid();` then `BMSSYS0000051988` is what I get back. Do you know if `uuid` should be set by the developer's own code, or should it be only set by Drupal Core itself?
cn flag
I've added my migration config to my question to show how uuid was written to with `BMSSYS0000051988`. If this is "illegal" in that only Drupal core should manage and create/write `uuid`s, then that would promote my strategy b) to implement a workaround to avoid using the uuid in this way: https://drupal.stackexchange.com/questions/310697/programmatically-get-child-nodes-referencing-parent-by-entity-reference-uuid-us?noredirect=1#comment395034_310699
4uk4 avatar
cn flag
No, it's not that only Drupal core should manage UUIDs. One of the main selling points is that UUIDs are unique world wide, so you can migrate them from site to site and they remain unique. But I wouldn't use the UUID field for other data than UUIDs. Put the non-UUID value in its own field and use UUIDs to cross reference data items in the source and/or destination of the migration. Where you create the UUIDs doesn't matter.
cn flag
+1 agree. I would prefer that the migration would not have imported that field data into `uuid` and instead another ordinary field. However I need to work with what I have, so in my 'Update 3', I will write a db query.
Score:0
cn flag

In theory this should work, the field type sets the UUID as main property: EntityReferenceUuidItem:: mainPropertyName()

It might be that Drupal is confused and handling the field as a standard entity reference field because it extends the core field type.

To make clear which field property you are querying append it with a dot:

'field_scientists.target_uuid' => $entity_reference_uuid

Which should work for both, loadByProperties or an entity query:

->condition('field_scientists.target_uuid', $entity_reference_uuid)

The first is only a shortcut for the second using the same query engine.

cn flag
I will try this out, this morning and get back to you - thank you so far.
cn flag
Unlucky I'm afraid. Array returned still empty.
cn flag
On basis that `condition` approach boils down to the same query engine, I would be inclined to not try that also, anticipating same issue instead do 2 things a) raise bug on https://www.drupal.org/project/entity_reference_uuid and b) implement a work around, create a new field in my content type which is a plain old entity reference (not with uuid), populate with a reference to a node and test the code again. If works, write script to populate rest of the nodes (there are 100K of them though). The populating will use the existing entity ref uuid field value to find the node for the new field.
4uk4 avatar
cn flag
Is there a way to reproduce the issue on a fresh install? I couldn't. For me it works with and without the appended property name.
4uk4 avatar
cn flag
b) as a work around I wouldn't generate extra data. I would write low level SQL instead. This is also a good test whether the UUID you are querying really exists. Low level this is a simple query on one table.
cn flag
Noted thank you @4uk4 . My setup seems strange. Looking at the entity reference uuid field in the node, it is a string, the value of which is from a field that was imported from a CSV. In edit mode display that field shows as the parent node title with its node id in brackets. At the moment, I'm puzzled how that string value can reference the parent node.Looking at the Entity Reference UUID module, I haven't found any config nor in the field that gives a clue. I don't know which field is designated as the UUID. Some missing pieces of the puzzle for me.
cn flag
I think I need to work out how to get a node's uuid to help me understand.
4uk4 avatar
cn flag
`$uuid = $node->uuid();`
cn flag
+1 thank you yes, @4uk4 and example `uuid` in my case for that is `BMSSYS0000051988` . I've updated my question also, with turther information as to what I'm tryng. Thank you for all your help.
cn flag
looks like it is a confusion of mine between the value of the data in an ordinary field and uuid. so your answer is valid and I agree should work. I will write up some further info later.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.