3

「/PARENT_TERM/TERM/NODE_NAME」のようなカスタム URL パスを保存しようとしています。

Pathauto のおかげで、URL 文字列を作成するために必要な情報を取得できましたが、保存する URL を取得できないようです。また、この情報を URL パス設定または URL エンティティに保存するのが最善かどうかもわかりません。

URL の作成に Pathauto を使用しない理由は、ノードのパターンを作成するときに、親用語のトークンが提供されないためです。

これまでの私のコードは次のとおりです。

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)
    }
}

ありがとうございました!

4

0 に答える 0