Skip to content

2.x Introduce is_target_blank method for MenuItem#1701

Merged
jarednova merged 3 commits into2.xfrom
2.x-menu-item-target
Apr 4, 2018
Merged

2.x Introduce is_target_blank method for MenuItem#1701
jarednova merged 3 commits into2.xfrom
2.x-menu-item-target

Conversation

@gchtr
Copy link
Copy Markdown
Member

@gchtr gchtr commented Apr 1, 2018

Ticket: #1629

Issue

There is no easy way to check whether the «Open link in new tab» option is checked on a menu item in the backend.

Solution

  • Introduce a new method MenuItem::is_target_blank() that checks whether the post meta entry _menu_item_target is set to "blank".
  • Add fallback return value of null for MenuItem::meta().
  • Add documentation for is_target_blank() and is_external() to use them in combination.

Impact

None.

Usage Changes

Item targets can now be checked with

<a href="{{ item.link }}" {{ item.is_target_blank ? 'target="_blank"' }}>

And in combination with MenuItem::is_external():

<a href="{{ item.link }}" {{ item.is_target_blank or item.is_external ? 'target="_blank"' }}>

Testing

I added a test to check for the different states:

  • No _menu_item_target meta value set on a menu item
  • Meta value "blank" for _menu_item_target.
  • Meta value "" for _menu_item_target.

@jarednova
Copy link
Copy Markdown
Member

Hey @gchtr, thanks as always for the PR and tests. I'm wondering if the {{ item.target }} approach you had suggested in #1629 might be a more universal solution though. The Twig result would be...

Item targets can now be checked with

<a href="{{ item.link }}" target="{{ item.target }}">

And in combination with MenuItem::is_external():

<a href="{{ item.link }}" {{ item.target is '_blank' or item.is_external ? 'target="_blank"' }}>

I realize it's a small distinction, but it seems (slightly) more versatile. While I agree with @gserafini that the possibilities are pretty limited, this would eliminate the need for a conditional in menus (only requiring it if someone wanted the external or blank logic). What say you?

@gchtr
Copy link
Copy Markdown
Member Author

gchtr commented Apr 4, 2018

@jarednova The only reason I didn’t choose to use target="{{ item.target }}" is that target="_self" would be an obsolete attribute. I also thought about having

<a href="{{ item.link }}" {{ item.target }}>

which could add target="_blank" if necessary, but throughout Timber, we only ever add the value of an attribute, and not a whole attribute (right ?). And I didn’t think about reducing conditionals in menus, which is a really good reason. So yeah, I see, we should probably go with:

<a href="{{ item.link }}" target="{{ item.target }}">

But why don’t we add both? Would we add redundancy if developers could still use item.is_target_blank()?

<a href="{{ item.link }}" {{ item.is_target_blank or item.is_external ? 'target="_blank"' }}>

And I see, before we merge this, I should probably update the menu guides and add target="{{ item.target }} where necessary.

@jarednova
Copy link
Copy Markdown
Member

I'm good with both! (and the gif that goes along with it). Let's do it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants