Similar to this question but for Drupal 8, I have a custom node type with field_product_category and field_project_number. I want to increment project number based on a count of nodes with a specific product category value, prior to saving the node. From what I'm finding, entity query seems to be the way but I'm confused on syntax, much less where to put the query. My current thought is something like:
$category = field_product_category_value; $count_query = $query->countQuery(\Drupal::entityQuery('node')) ->condition('type', 'part') ->condition('status', '0') ->condition('field_product_category_value', '$category') execute(); $project_num = $count_query++; node->set('field_project_number', "printf("%03s\n", $project_num)"); node->save();
With my limited knowledge, I know this is not correct but haven't found suitable examples to replicate. Also, after lots of googling, where would I actually place this query? In a custom module? Thanks in advance to anyone that can help.