1

I have a vocabulary "holiday_types" with taxonomy terms "ski" and "beach". I have a custom field on these taxonomy terms "field_activityid" which relates to a legacy system.

I want to load all these terms so that as I loop through a list of regions I can add the taxonomy_term ID to an entity reference field, and I need to get the legacy activityid to query an external API.

I'm having issues with entityQuery not recognising 'holiday_types' as a bundle of taxonomy term, I tried 'type' too. I'm also not getting entityQueryAggregate working either so thought I'd pose the question here to see if there's a 'proper' way of querying custom fields on taxonomy terms.

Adding example here which produces "QueryException: 'bundle' not found"

$holidayTypesQuery = \Drupal::entityQueryAggregate('taxonomy_term');
$holidayTypesQuery->conditionAggregate('bundle', 'holiday_types');
$ids = $holidayTypesQuery->execute();
$storage = \Drupal::entityManager()->getStorage('taxonomy_term');
$entities = $storage->loadMultiple($ids);

If I change 'bundle' to 'vid' it errors with the following:

    Drupal\Core\Database\DatabaseExceptionWrapper: SQLSTATE[42000]: 
Syntax error or access violation: 1305 FUNCTION purkidev.holiday_types does not exist: 
SELECT holiday_types(taxonomy_term_field_data.vid) 
AS vid_holiday_types 
FROM {taxonomy_term_data} base_table 
LEFT JOIN {taxonomy_term_field_data} taxonomy_term_field_data 
ON taxonomy_term_field_data.tid = base_table.tid 
INNER JOIN {taxonomy_term_field_data} taxonomy_term_field_data_2 
ON taxonomy_term_field_data_2.tid = base_table.tid 
HAVING (holiday_types(taxonomy_term_field_data_2.vid) = :db_placeholder_0); Array ( [:db_placeholder_0] => ) 
in Drupal\purkitest\Controller\PurkitestController->simple() (line 89 of /Users/steve/Sites/purkidev/modules/purkitest/src/Controller/PurkitestController.php).

Trying without Aggregate:

$holidayTypesTIDs = \Drupal::entityQuery('taxonomy_term')
  ->condition('vid', 'holiday_types')->execute();
$taxonomyTermStorage = \Drupal::entityManager()->getStorage('taxonomy_term');
$holidayTypes = $taxonomyTermStorage->loadMultiple($holidayTypesTIDs);
foreach ($holidayTypes as $holidayType) {
  dpm($holidayType);
}

This works but when I dpm($holidayTypes) I don't get anything in the fields:

[fields:protected] => Array
    (
    )

...hence why I was trying aggregate as I thought it brought together the fields.

So, can I access the value of my field_activityid from here and if so how? Have tried various random things like

dpm($holidayType->field_activityid->value()) 

which produces

Call to undefined method Drupal\Core\Field\FieldItemList::value()

Following berdir's comments here's the latest whole function... which now works - thanks all for helping my syntax!

public function simple() {

$holidayTypesTIDs = \Drupal::entityQuery('taxonomy_term')
  ->condition('vid', 'holiday_types')->execute();
$taxonomyTermStorage = \Drupal::entityManager()->getStorage('taxonomy_term');
$holidayTypes = $taxonomyTermStorage->loadMultiple($holidayTypesTIDs);

$markup = '';
foreach ($holidayTypes as $holidayType) {
  $markup .= "<p>Holiday Type: ";
  $markup .= $holidayType->getName();
  $markup .= "</p><p>TID: ";
  $markup .= $holidayType->id();
  $markup .= "</p><p>ActivityID: ";
  $markup .= $holidayType->field_activityid->value;
  $markup .= "</p><p>";
}

return array(
  '#markup' => $markup,
);

}

  • I think it is going to be difficult to qualify "entityQuery not recognising" and "not getting entityQueryAggregate working" without a specific example. I would look at the annotation for Term entity type to see what the bundle key is, which is 'vid' not 'type' or whatever you tried first. – mradcliffe Dec 29 '15 at 14:45
  • ok added example, sorry, stuck a little too deep inside my own head right now lol - thanks for your time & attention @mradcliffe! – stevepurkiss Dec 29 '15 at 14:55
  • I think it should be $holidayTypesQuery->conditionAggregate('vid', 'holiday_types');. I this is because it's not synonymous to entityCondition() like in Drupal 7, but is more aligned toward the raw database abstraction layer. But I don't have an environment to confirm other than EntityQueryAggregateTest. – mradcliffe Dec 29 '15 at 15:01
  • thanks, posted results of that change above, will go read code from EntityQueryAggregateTest now. Not even sure I need the aggregate query but am gathering from what I read that aggregate means the config entity fields/attributes /and/ the custom fields on the bundle are returned in one query. – stevepurkiss Dec 29 '15 at 15:27
  • Oops, I see, conditionAggregate method takes a SQL function as the second argument and the value as the third argument. I'm not sure what SQL function you would need for that query. Maybe just do a regular Entity Query and condition off of 'vid'? – mradcliffe Dec 29 '15 at 15:54
2

Your entity query works fine (without aggregate). dpm() doesn't work in D8 for content entities, try kint() instead, but that will also be misleading.

Your last attempt was almost correct: ->value is a property (a magic property, actually), not a method. So this should work fine:

$holidayType->field_activityid->value

As referenced already in a few questions, have a look at http://wizzlern.nl/drupal/drupal-8-entity-cheat-sheet; it's a bit old but it's still a great overview of the Content Entity (Field) API.

If you do want to use dpm()/kint(), then always run it on $entity->toArray(). That will give you an array structure of the fields, you can translate that 1:1 to object properties and it will work fine. For deltas, use $holidayType->field_activityid[1]->value; not providing it defaults to delta 0.

And, no, you definitely don't want an aggregate query. You need that if you want to COUNT() or SUM() some values of your entities.

  • Thanks, that worked! I thought at first it didn't but then realised I'd lost my values somehow so reimported the data and it's now showing. I also understand aggregate now as an aggregate of all the results of the query, not the contents of the fields etc. – stevepurkiss Dec 29 '15 at 17:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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