I am trying to override node links by using hook_node_view_alter()
, so my code looks like:
$cancel_link = 'admin/some_path/'.$found_id.'cancel';
$build['links']['node']['#links']['cancel'] = array(
'title' => t('Cancel'),
'href' => $cancel_link,
'html' => TRUE,
'attributes' => array(
'title' => 'Cancel',
),
);
and it works fine. But I also need to add "?destination=URL" to links and unfortunately, trying several different approaches didn't work for me. In particular:
I copied the whole Good Example code from https://www.drupal.org/docs/7/theming/overriding-themable-output/how-to-add-a-destinationurl-to-the-request-new-password and replaced my code with:
$build['links']['node']['#links']['cancel'] = l(t('Request new password'), 'user/password', array('query' => array('destination' => 'homepage', 'foo' => 'bar'), 'attributes' => array('title' => t('Request new password via e-mail.'))));
but the link didn't show up at all.
Example on https://www.zyxware.com/articles/5195/how-to-set-destination-as-the-link-value-in-the-l-in-case-of-urls-with-parameters didn't work.
Tried to adapt the accepted answer on use URL with query as URL destination as:
$query_string = array('destination' => 'service/922');
$url = url($cancel_link, array('query' => $query_string));
$build['links']['node']['#links']['cancel'] = array(
'title' => t('Cancel'),
'href' => $url,
'html' => TRUE,
'attributes' => array(
'title' => 'Cancel',
),
);
unfortunately, it is also giving %3Fdestination%3Dservice/922
in the browser address bar. What I am doing wrong here?
Addition: Using dpm() I've figured out the above examples all output ready to use links like <a href="/user/password?destination=homepage&foo=bar" title="Request new password via e-mail.">Request new password</a>
However, if I dpm the link in my function it still gives raw array:
(Array, 4 elements)
title (String, 6 characters ) Cancel
href (String, 33 characters ) admin/some_path/id/cancel
html (Boolean) TRUE
attributes (Array, 1 element)
title (String, 6 characters ) Cancel
And I wonder is it possible to add destination at this stage at all or do I have to intercept the link at a later steps like for example while themeing?