Simple request and should be possible with hook_block_view_alter() - perform token replacement on $block->title.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Dave Reid’s picture

Status: Active » Needs review
FileSize
1.35 KB
Dave Reid’s picture

With tests this time.

Dave Reid’s picture

Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Triskelion’s picture

I tried to use this capability with Welcome [user:name] in a block title, and it simply returned the text.

Had to add global $user; to token_block_view_alter(), and change the call to token_replace to $block->title = token_replace($block->title, array( 'user' => $user ), array('sanitize' => FALSE)); to get it to work.

It is necessary to decide which tokens this function should support, and to pass the required $data array to token_replace().

Dave Reid’s picture

You should be using [current-user:name]. The token [user:name] only makes sense when provided with a specific user.

Triskelion’s picture

Kudos! I have never had such a rapid response.

Point taken. However, if you are going to restrict the use to system tokens, it might be a good idea to add a token_tree element to the settings. The only list of tokens now available is at admin/help/token.

function token_form_block_admin_configure_alter(&$form, $form_state) {
  $form['settings']['title']['#description'] .= ' ' . t('This field supports tokens.');
  // @todo Figure out why this token validation does not seem to be working here.
  $form['settings']['title']['#element_validate'][] = 'token_element_validate';
  $form['settings']['title']['#token_types'] = array();
  $form['settings']['token_tree'] = array(
      '#theme' => 'token_tree',
      '#token_types' => array(),
      '#weight' => $form['settings']['title']['#weight'] + 0.5,
    );
}

Token validation is not working because the #token_types array is empty, and token_get_invalid_tokens_by_context is bypassed (This might be a bug, since system tokens will never be validated if they are the only context permitted.).

Great module, and thank you for the work you have done.

Dave Reid’s picture

Status: Closed (fixed) » Active

Hrm, yeah it would be a bug if that validation is getting bypassed. It should still run for global token types.

Dave Reid’s picture

Status: Active » Needs review
FileSize
1.67 KB

Patch attached that ensure that if #token_types is set but to an empty array, it should still run token validation.

Dave Reid’s picture

Status: Needs review » Fixed

Committed #9 to Git. http://drupalcode.org/project/token.git/commit/f9016e7

As for the token browser, I've been holding off adding the browser to new places due to the performance concerns of having the browser everywhere. Once we solve #1684984: Support opening token browser in a jQuery UI dialog on demand we'll be able to add the token browser into this.

Triskelion’s picture

Thanks again Dave. I've started following the token browser issue.

jlongbottom’s picture

Node tokens do not seem to be working, but Current Page tokens do.

ex:

[node:title] - not working

[current-page:url:args:value:1] - working

Dave Reid’s picture

As expected. There's nothing currently that we do to determine what types of data we have in a reliable fashion to pass into token_replace() in this context aside from global non-context tokens like current-page.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

kalabro’s picture

Component: Code » User interface
Status: Closed (fixed) » Needs review
FileSize
3.76 KB
709 bytes

Very cool! Could we make it more easy for site builders to see which tokens are supported?
Looks good with admin theme fix: #1859452: Dialog loads default theme CSS breaking admin theme

Dave Reid’s picture

Status: Needs review » Closed (fixed)

@kalabro: Can you please post this as a follow-up separate issue rather than re-opening an issue that's already been closed for half a year?

francescosciamanna’s picture

#6 Solution works great for me!
[current-user:name] it's enough to do the job in the Block title!

Status: Closed (fixed) » Needs review

Status: Needs review » Needs work

The last submitted patch, 15: token_block-title-ui_15.patch, failed testing.

Dave Reid’s picture

Issue summary: View changes
Status: Needs work » Closed (fixed)

Status: Closed (fixed) » Needs review

Status: Needs review » Needs work

The last submitted patch, 15: token_block-title-ui_15.patch, failed testing.

Dave Reid’s picture

Status: Needs work » Closed (fixed)