3

I'm trying to save a custom url path like such: "/PARENT_TERM/TERM/NODE_NAME"

Thanks to Pathauto, I've managed to get hold of the info I needed to construct the url string but I can't seem to get my url to save. I'm also not sure whether it would be best to save this information on the Url Path Settings or in URL entity.

The reason I'm not using Pathauto to make the url is that it doesn't provide any tokens for the parent term when I'm making a pattern for my nodes.

Here is my code so far:

function HOOK_node_insert($entity) {
    _HOOK_node_url($entity, 'insert');
}

function HOOK_node_update($entity) {
    _HOOK_node_url($entity, 'update');
}

function _HOOK_node_url($entity, $op){
    if($entity->getType() == 'dvn_products'){ //Content_type
        $nid = $entity->id();

        $entity_alias = \Drupal::service('path.alias_manager')->getAliasByPath('/node/' . $nid); // Get the pathauto alias

        $term_id = $entity->field_dvn_product_type_ref->target_id;
        $term_object = \Drupal\taxonomy\Entity\Term::load($term_id);
        $term_name = $term_object->get('name')->value;
        $term_alias = \Drupal::service('path.alias_manager')->getAliasByPath('/taxonomy/term/' . $term_id); // Gets pathauto term alias

        $new_url = str_replace("/".strtolower($term_name),$term_alias,$entity_alias); //replaces child term with its url

        $entity->path->alias = $new_url; // Saves new url (This is the problem)
    }
}

Thank you!

Andreas
  • 1,691
  • 1
  • 15
  • 34

0 Answers0