0

So I'm having a bit of a difficult time finding out how to get the information I want.

I have an entity reference field - call it field_event_type. This is an entity reference to a taxonomy. I am trying to programmatically get the possible vocabularies associated with this entity reference and get the fields/properties associated with them. Basically walking each of the bundles to get their relevant information.

So far I can get the field definition on the parent node, which looks like this:

Array
(
    [uuid] => 32eeda5f-270f-477d-b24c-75ed15e69b2a
    [langcode] => en
    [status] => 1
    [dependencies] => Array
        (
            [module] => Array
                (
                    [0] => node
                    [1] => taxonomy
                )

        )

    [id] => node.field_event_type
    [field_name] => field_event_type
    [entity_type] => node
    [type] => entity_reference
    [settings] => Array
        (
            [target_type] => taxonomy_term
        )

    [module] => core
    [locked] => 
    [cardinality] => 1
    [translatable] => 1
    [indexes] => Array
        (
        )

    [persist_with_no_fields] => 
    [custom_storage] => 
)

By doing this:

\Drupal::config('field.storage.node.field_event_type')->get();

Based on this link:

field_info_field() deprecated, what's the equivalent?

But I'm not sure how to get the vocabularies or even if that's the right thing to do to get the information I want.

How would I go about getting the vocabularies? I should be able to just load by VID and get the correct fields at that point, correct?

  • Solutions mentioned do not work. Can someone provide solution for this? – neha Jun 19 '17 at 12:26
0

So here is my process (it won't work when you're loading in the relevant entity types via a view, only by the "default" method):

$type is the content type, $field is the field of the ER.

$field_data = \Drupal::config('field.field.node.' . $type . "." . $field)->get();

$bundles = $field_data['settings']['handler_settings']['target_bundles'];

At which point you can iterate over the given bundles for an ER.

You'll probably want to do some flow control somewhere to ensure the ER has the correct handler you want - located here:

$handler = $field_data['settings']['handler'];

But this should work to get the data I asked for above.

  • Use $field_definition = \Drupal::service('entity_field.manager')->getFieldDefinitions('node', $type)[$field], then $field_definition->getSetting('handler') instead of going through the low-level config API – Berdir Feb 8 '17 at 23:46
  • Both the solutions do not work. Can someone provide solution for this? – neha Jun 19 '17 at 12:25
0

I too was trying to sort out how to get the vocabulary from the entity reference field for a node type. Here's code that seems to work for me - returns an array keyed on fieldname with vocabulary as the value. I check out the field handler and flag ones with "default:taxonomy" as taxonomy reference fields. Then I loop through dependencies['config'] to see if taxonomy.vocabulary is referenced and get the value.

$bundle = 'page';
$vocs = [];
$flds = \Drupal::service('entity_field.manager')
  ->getFieldDefinitions('node', $bundle);
foreach ($flds as $fld) {
  $hand = $fld->getSettings()['handler'];
  if ($hand == "default:taxonomy_term") {
    $fldnm = $fld->getName();
    $config = $fld->get('dependencies')['config'];
    foreach($config as $cf){
      if(strpos($cf,"taxonomy.vocabulary") !== FALSE){
        $vocs[$fldnm] = str_replace("taxonomy.vocabulary.","",$cf);
      }
    }
  }
}
dpm($vocs);
0

You can also see the structure from config file Filename: field.field.node.article.field_tags.yml config export

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.