2

I want to alter url of a field and his display. The field is a taxonomy label with link to the entity. The default url is : taxonomy/term/{tid}

I wanto to chage with : my_specific_page?technologie={tid} (used to prepopulate exposed filter)

I tried with :

function MYTHEME_preprocess_field__field_technologie(&$variables, $hook){
  $element = $variables['element'];

  foreach ($variables['items'] as $item) {

    $route_name = 'entity.node.canonical';
    $params     = 39;

    $options = [
      'query' => ['technologie' => 27],
    ];
    $url = Url::fromRoute($route_name, array('node' => $params), $options);

    $item['content']['#url'] = $url;
  }
}

I think i need to replace routeName attribut, but how ?

Content type : "manage field" Content type : "manage field" Content type : "manage display" Content type : "manage display" Kint result $variables['element'] [kint]3

  • I can't get the point, can you improve a little the question? – Adrian Cid Almaguer Oct 11 '17 at 16:17
  • Ok, I added pictures and changed the title for more understanding. – florian_drupal Oct 12 '17 at 8:05
3

I found solution :

function MYTHEME_preprocess_field__field_technologie(&$variables, $hook){
  $element = $variables['element'];

  foreach ($variables['items'] as $index => $item) {

    $route_name = 'entity.node.canonical';
    $params     = 39;
    $tid = $item['content']['#options']['entity']->id();

    $options = [
      'query' => ['technologie' => $tid],
    ];

    $url = Url::fromRoute($route_name, array('node' => $params), $options);
    $variables['items'][$index]['content']['#url'] = $url;
  }
}

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.