Pregunta

Ok, so I'm trying to populate a field based on the selection of a taxonomy term. I've mainly got it working thanks to this stack exchange article

use Drupal\taxonomy\Entity;

function module_entity_presave(EntityInterface $entity) {

  if($entity->bundle() == "node_type"){
    $bread_slice = $entity->get("field_taxonomy_term")->getString();
    $bread_butter = "- Additional Text for Reasons";
    $bread_title = $bread_slice." ".$bread_butter;
    $entity->field_title->value = $bread_title;
    //save is done automatically
  }  

}

The issue is that, the resulting code doesn't print the name/label of the term, but instead the tid. So, it ends up showing as "757 - Additional Text for Reasons" I barely know any php, but I know enough to get around. I suppose my issue is the fact I'm not getting the correct value from the taxonomy array, yet I'm unsure how. I've tried finding the correct value using some twig debugging, {{dd(data.taxonomy_term)}} but all the values for it show null. Please point me in the direction of how to solve this?

¿Fue útil?

Solución

The field only stores term IDs. So either get the term ID and load the term from it or load the referenced entities directly.

/**
 * Implements hook_entity_presave().
 */
function MYMODULE_entity_presave(\Drupal\Core\Entity\EntityInterface $entity) {

  if ($entity instanceof \Drupal\node\NodeInterface && $entity->bundle() === 'MYBUNDLE') {

    if ($field = $entity->get('field_MYFIELD')) {
    
      if (!$field->isEmpty()) {

        $label = $field->referencedEntities()[0]->label();
        // dpm($label);
        // Code on...
      }
    }
  }
}

Consider installing Devel. It gives you some handy debugging helpers like dpm($some_var) to dump variables into core's status messages block so you can inspect them easily.

Otros consejos

You can get the entity from entity reference fields like:

$entity->field_name->entity // or `$entity->get('field_name')->entity` whichever you prefer

if single value, or loop with referencedEntities() method on the field like:

foreach ($entity->field_name->referencedEntities() as $term)
// Or foreach ($entity->get("field_name")->referencedEntities() as $term)

if multi-value (or if you want to keep your code more "general purpose" as this also works for single value fields). Also check if there's anything to work with or exit early, so all together something like:


function module_entity_presave(EntityInterface $entity) {
  if ($entity->getEntityTypeId() !== 'node' || $entity->bundle() !== 'node_type') {
    // Not an entity or bundle we're concerned with.
    return;
  }

  if ($entity->get("field_taxonomy_term")->isEmpty()) {
    // No term to work with.
    return;
  }
  /** @var \Drupal\taxonomy\TermInterface $term */
  $term = $entity->get("field_taxonomy_term")->entity;
  // Translate the text. Term label should be already translated value so pass it
  // as a param.
  $entity->field_title->value = t('@label - Additional Text for Reasons', [
    '@label' => $term->label(),
  ];
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a drupal.stackexchange
scroll top