0

Using Drupal 8 entity query I'm trying to figure how I can fetch values from a specific content type and group them that are associated via a specific taxonomy term.

As seen in the screenshot below I have an entity field called Group Schedule that is currently binded to a Taxonomy Term. Is there way I could group those entities that are assigned to a specific taxonomy?

Group Schedule Name (field_group_sched)
First Schedule (field_created_date)
Second Schedule (field_created_date)

Group Schedule Name 2 (field_group_sched)
First Schedule (field_created_date)
Second Schedule (field_created_date)

enter image description here

So far I've managed to fetch the values from the Schedule Content type

// Entity Query for Schedule Node Type
$scheduleType = $query->get('node')
    ->condition('status', 1, '=')
    ->condition('type', 'schedule')
    ->condition('field_associated_programs', $nid_degree, '=')
    ->sort('created')
    ->execute();

foreach ($scheduleType as $key => $program) {

    $programNode = _nodeLoad($program);

    $variables['schedule_content'][$key]['schedule_group'] = $programNode->get('field_group_sched')->value;
    $variables['schedule_content'][$key]['schedule_name'] = $programNode->get('field_schedule_name')->value;
    $variables['schedule_content'][$key]['schedule_date'] = $programNode->get('field_created_date')->value;


}

{% for program in schedule_content %}

<div class="scheduled-dates">
    <h3 class="label">{{ program['schedule_group'] }}</h3>
    <span data-label="{{ program['schedule_name'] }}"  class="date" >{{ program['schedule_date'] }}</span>
    </div>


{% endfor %}
5
  • 1
    Can you explain more on how you would like to group them please? aka for is this for form values or for info display ? also why are you using custom code? as can do this with views in like 1 min then make a block and throw it where you need it ?
    – taggartJ
    Commented Jan 31, 2018 at 3:06
  • I'm trying to group them by using the Group Schedule field as shown in the screenshot. Lets say I have to two nodes that are associated with a specific taxonomy. I want to group those two nodes and output them on the twig using custom code.
    – Noel
    Commented Jan 31, 2018 at 3:14
  • 1
    Sweet your almost there, were is this on a node display or custom controller or a custom block or on a taxonomy term page ?
    – taggartJ
    Commented Jan 31, 2018 at 3:42
  • The first block of code is basically the entity query that is created on the .theme file. The second block of code is the markup on how I can display the output on the twig file
    – Noel
    Commented Jan 31, 2018 at 3:44
  • 1
    ok give me a sec i have never tried to do this in a .theme file , usually this kind of stuff is done in a custom module in a controller or block ect.
    – taggartJ
    Commented Jan 31, 2018 at 3:50

1 Answer 1

1

Ok well here we go if i understand you correct you want nodes linked by a entity reff field value: in your example "field_group_sched" the see the below condition but later you can just group stuff by anything you want

   // this is the known term id. 
    $group_term_id = 1;
    // for Entity ref use field_name.target_id
    $query = \Drupal::entityQuery('node')
      ->condition('status', 1, '=')
      ->condition('type', 'schedule')
      ->condition('field_group_sched.target_id', $group_term_id, '=')
      ->sort('created');

    $entity_ids = $query->execute();
    if (count($entity_ids) != 0) {
      $entities = \Drupal::entityTypeManager()
        ->getStorage('node')
        ->loadMultiple($entity_ids);
      // dump($entities); // requires var dummper
      if (count($entities) != 0) {
        $group1 = [];
        $group2 = [];
        // then you can play with order or whatever you want here. 
        foreach ($entities as $node) {
          // These are the loaded nodes;
          $title = $node->getTitle();
          $field_name  = $node->field_schedule_name->value;
          // if the schedule_name is "beer" put in to group 1
          if ($field_name == 'beer') {
            $group1[] = $node;
          }
          else {
            $group2[]= $node;
          }
        }
        // SEND your Nodes to the template here. 
      }
    }
4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.