Problem/Motivation

There should be a link to open the reference to available tokens in the Configure block dialogue.
That way you can consult it, when you want to add a token into the block title, for example.

Proposed resolution

Add code to generate the link into the token_form_block_form_alter function and adjust the weight to position it below the text field.

Remaining tasks

User interface changes

Link will be displayed below the text field, if possible.

API changes

Data model changes

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

VladimirMarko created an issue. See original summary.

VladimirMarko’s picture

Assigned: Unassigned » VladimirMarko
AaronBauman’s picture

I came here to post a patch for the same issue.
Let's get it fixed!

Did you assign to yourself because you're working on a patch?
Otherwise, you should unassign to indicate that it's ready to be picked up by anybody.

Berdir’s picture

Assigned: VladimirMarko » Unassigned

He did, but he also left for the weekend already. If you have a patch, post it.

AaronBauman’s picture

ok, here it is

Berdir’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

That's... an interesting use of #description.

Can you post a screenshot of how that looks?

Also needs tests

AaronBauman’s picture

I was surprised that this worked at all:

The 'browse' link is appended to div.description

The 'browse' link is appended to div.description

The problem with a more standard approach is that "Title" field doesn't have a #weight during form_alter, so it's not straightforward to situate a new element between the first 2 fields.
This approach was more expedient than, e.g. implementing a process callback.

What would a test look like for this?
There aren't any tests for the other 'browse' link implementations for me to crib.

Berdir’s picture

That looks quite nice. thanks. The problem with it might be that it won't work so well for a right to left language like russian. But probably not a huge deal if the link is before the text.

We have \Drupal\token\Tests\TokenBlockTest already. So basically, what we need to do is on the page where we edit the block, that we add an assertLink() that makes sure the link is displayed there. So it should be like a single additional line.

VladimirMarko’s picture

I am missing the space between "tokens." and "Browse", when I apply the patch.
It says "This field supports tokens.Browse available tokens."

Berdir’s picture

Yes, I suspected this would happen.

We could try something like t('This field supports tokens. @browse_tokens_link') and then pass in the render array like that, but we probably need to render it ourself then with \Drupal::service('render')->render()

VladimirMarko’s picture

Fixed by using a placeholder as suggested.
There was no need to use render().

Also updated the associated test with two lines of assertions.

Status: Needs review » Needs work

The last submitted patch, 11: browse_available-2834366-11.patch, failed testing.

The last submitted patch, 11: browse_available-2834366-11.patch, failed testing.

VladimirMarko’s picture

Added the \Drupal::service('renderer')->render() call. There are no more exceptions in my local test.

The last submitted patch, 15: browse_available-2834366-14-test-only.patch, failed testing.

The last submitted patch, 15: browse_available-2834366-14-test-only.patch, failed testing.

Berdir’s picture

Status: Needs review » Needs work
Issue tags: -Needs tests
  1. +++ b/src/Tests/TokenBlockTest.php
    @@ -48,6 +48,9 @@ class TokenBlockTest extends TokenTestBase {
         $this->drupalGet($block->urlInfo());
    +    // Ensure that the link to available tokens is present and correctly positioned.
    +    $this->assertLink('Browse available tokens.'); //@todo
    

    Comment is too long.

    Remove @todo left-over.

  2. +++ b/token.module
    @@ -86,7 +86,15 @@ function token_block_view_alter(&$build, BlockPluginInterface $block) {
    -  $form['settings']['label']['#description'] = t('This field supports tokens.');
    +//  $form['settings']['label']['#description'] = t('This field supports tokens.');
    

    remove the old commented out line.

  3. +++ b/token.module
    @@ -86,7 +86,15 @@ function token_block_view_alter(&$build, BlockPluginInterface $block) {
    +  $token_tree = array(
    

    use short array syntax

  4. +++ b/token.module
    @@ -86,7 +86,15 @@ function token_block_view_alter(&$build, BlockPluginInterface $block) {
    +  $form['settings']['label']['#description'] = array(
    +    '#prefix' => t('This field supports tokens. @browse_tokens_link', array('@browse_tokens_link' => $rendered_token_tree))
    

    #prefix stuff isn't needed, just = t(...);

The last submitted patch, 19: browse_available-2834366-20-test-only.patch, failed testing.

The last submitted patch, 19: browse_available-2834366-20-test-only.patch, failed testing.

AaronBauman’s picture

Nice, looks good to me.

  • Berdir committed 5dac702 on 8.x-1.x authored by VladimirMarko
    Issue #2834366 by VladimirMarko, aaronbauman: "Browse available tokens...
Berdir’s picture

Status: Needs review » Fixed

Agreed, committed.

Status: Fixed » Closed (fixed)

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