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')

$project_num = $count_query++;
node->set('field_project_number', "printf("%03s\n",  $project_num)");

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.

  • Try hook_entity_presave – berramou Dec 5 at 15:13
  • @berramou, thanks for the reply. That would have to be called in a custom module, correct? – johnny_fear Dec 5 at 15:44
  • you are welcome, Yes exactly in custom module. – berramou Dec 5 at 15:50

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.