Problem/Motivation

Currently, the flag links are rendered directly and without the use of the theme layer. This makes it difficult to style flag links.

Proposed resolution

Implement hook_theme and a theme function or template for flag links.

Remaining tasks

Create patch.

User interface changes

None, although an additional CSS class may be applied to the flag link.

API changes

Flag links would be themeable.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

joachim’s picture

Title: Implement themeing for flag link » Implement theming for flag link

There's a template on 7.x that we should use as a starting point, though we should also trawl through the issue queue as there have been various issues over the years to do with limitations of the theming approach.

socketwench’s picture

After some discussion on Twitter, the preferred way forward for D8 is to implement a Twig template for nearly all theming as opposed to theme functions. Since we used a template before, I see no reason to change that.

Unfortunately, there doesn't seem to much for Twig in Examples. Core book module seems to have some promise to figure out how to use Twig.

joachim’s picture

I've been doing a fair bit with twig over on Garland. It's actually pretty easy!

joachim’s picture

Component: User interface » Flag core
Category: Feature request » Task
Priority: Normal » Critical

The D7 version of the theming for this was removed with commit eaa98bf807b1ed60557ee6be0582cb0a36b4604d -- that should be studied when tackling this issue.

martin107’s picture

just mapping eaa98bf807b1ed60557ee6be0582cb0a36b4604d back to an issue

#2651502: Remove flag.js

and patch

https://www.drupal.org/files/issues/2651502.7.removeThemeFolder.patch

martin107’s picture

I have taken a little look at this tried assembling pieces of the solution together

and I find myself coming up short perhaps someone can nudge me in the right direction.

here is the html of typical link

<a href="/flag/flag/flag_one/1?destination=node/1&amp;token=VuC2C-Q1BoU21Rg54EPwcYvrCkN5w6lFOobPxN_FbQ8" id="flag-flag_one-id-1">Flag this item</a>

here is the point in ActionLinkTypeBase in which the render array is generated and then modified.

  public function buildLink($action, FlagInterface $flag, EntityInterface $entity) {
    $url = $this->getLinkURL($action, $flag, $entity);

    $url->setRouteParameter('destination', $this->getDestination());

    $render = $url->toRenderArray();
    $render['#type'] = 'link';
    $render['#attributes']['id'] = 'flag-' . $flag->id() . '-id-' . $entity->id();

    if ($action === 'unflag') {
      $render['#title'] = $flag->getUnflagShortText();
      $render['#alt'] = $flag->getUnflagLongText();
    }
    else {
      $render['#title'] = $flag->getFlagShortText();
      $render['#alt'] = $flag->getFlagLongText();
    }

    return $render;
  }

so we could swap out this line

 $render['#type'] = 'link'; 

and replace it with a new #theme' called say 'flag-action-link' and then we could do what we want in corresponding twig file.

BUT below I am including the render array as it stands today....

The question I cannot answer is how can we deal with token part html the

token=VuC2C-Q1BoU21Rg54EPwcYvrCkN5w6lFOobPxN_FbQ8"

from the another perspective deal with #access_callback

Any prods in the right direction would be appreciated.

array(7) {
  ["#url"]=>
  object(Drupal\Core\Url)#994 (11) {
    ["urlGenerator":protected]=>
    NULL
    ["urlAssembler":protected]=>
    NULL
    ["accessManager":protected]=>
    NULL
    ["routeName":protected]=>
    string(19) "flag_link_flag.html"
    ["routeParameters":protected]=>
    array(3) {
      ["flag"]=>
      string(8) "flag_one"
      ["entity_id"]=>
      string(1) "1"
      ["destination"]=>
      string(6) "node/1"
    }
    ["options":protected]=>
    array(0) {
    }
    ["external":protected]=>
    bool(false)
    ["unrouted":protected]=>
    bool(false)
    ["uri":protected]=>
    NULL
    ["internalPath":protected]=>
    NULL
    ["_serviceIds":protected]=>
    array(0) {
    }
  }
  ["#options"]=>
  array(0) {
  }
  ["#access_callback"]=>
  array(2) {
    [0]=>
    string(15) "Drupal\Core\Url"
    [1]=>
    string(12) "renderAccess"
  }
  ["#type"]=>
  string(4) "link"
  ["#attributes"]=>
  array(1) {
    ["id"]=>
    string(18) "flag-flag_one-id-1"
  }
  ["#title"]=>
  string(14) "Flag this item"
  ["#alt"]=>
  string(0) ""
}
Berdir’s picture

Related: http://drupal.stackexchange.com/questions/188237/how-do-i-customize-the-... for possible use cases.

I think we should keep the #type, there would be quite a bit of code to copy otherwise.

Maybe we could specify a wrapper template that by definition just prints the link? If we can find a nice way to add some classes and other attributes to the link itself then that should be enough?

joachim’s picture

One thing we need to check is the confirmation messages that JS flags show on D7.

socketwench’s picture

Maybe we could specify a wrapper template that by definition just prints the link? If we can find a nice way to add some classes and other attributes to the link itself then that should be enough?

That's basically what I was thinking we would do from the start...

socketwench’s picture

Status: Active » Needs review
FileSize
2.07 KB

Here's a first go at implementing the template.

martin107’s picture

Status: Needs review » Reviewed & tested by the community
FileSize
1.98 KB
851 bytes

I think the patch from #10 is a good solution.

In #6 I included the generated html for the link ... below I am including what it looks like with the patch applied - and theme debug turned on.
The token comes through correctly and the only difference I can see is the inclusion of a empty alt attribute.

My patch is trivial - it deletes spaces from an empty line and adds a newline to the end of a file.

<!-- THEME DEBUG -->
<!-- THEME HOOK: 'flag' -->
<!-- BEGIN OUTPUT from 'modules/flag/templates/flag.html.twig' -->
<a href="/flag/flag/first_flag/1?destination=node/1&amp;token=C6otb7-pu_-r6_2bzZUsSHCI_Ywh0v5ZNYrbqfxDPX0" id="flag-first_flag-id-1" alt="" class="flag">Flag this item</a>

<!-- END OUTPUT from 'modules/flag/templates/flag.html.twig' -->

  • socketwench authored 2f76f74 on 8.x-4.x
    Issue #2463365 by martin107, socketwench: Added theming for flag link.
    
socketwench’s picture

Status: Reviewed & tested by the community » Fixed

Thanks for the fixes, Martin!

martin107’s picture

Ah thanks for teaching me the token trick.

Status: Fixed » Closed (fixed)

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