1

I want to show a link when a node is published.

I added the Content: Publishing status field and hid it. In global custom text, I added the following code.

{% if status == 0 %}
  <div class="apply_job"><a href="/appy-job" target="_blank">Apply</a></div>
{% endif %}

The replacement pattern for the published status is {{ status }}, which shows 0 or 1.

I am using Drupal 8.2.0.

Can anybody help me to make this work?

2 Answers 2

0

I tried that and it worked for me.

enter image description here

Just make sure to set the output format to 0/1.

{% if  status  %}
  <div class="apply_job"><a href="/appy-job" target="_blank">Apply</a></div>
{% endif %}

screenshot

5
  • thanks for the reply, but same code is not working in mine.My status field output format is boolean only. If node is unpublished also link is getting displayed. I have updated my question with the version of drupal 8.
    – rks
    Mar 31, 2017 at 4:01
  • @rks what you mean, you can see in my answer my field is boolean too and my drupal version in 8.2.6. do you want not exclude status field from display?
    – Yuseferi
    Mar 31, 2017 at 6:26
  • With {% if status %} the link would be shown also for unpublished nodes. I take the view is showing every node, independently for their status. Differently, checking the node status before outputting the link wouldn't make sense.
    – apaderno
    Aug 22, 2021 at 13:13
  • @apaderno, new account name, cool! if you put the output format on 0/1 then status 0 is equal false.
    – Yuseferi
    Aug 22, 2021 at 13:37
  • But status is a Drupal\Core\Render\Markup instance, not a number. I have set the Content: Published field to use Boolean as formatter and 1 / 0 as output format, and the code shown in this question doesn't work, as it shows the link independently from the node status. (I am confirming what @rks said in his comment.)
    – apaderno
    Aug 22, 2021 at 14:01
0

The correct code to use for the Global: Custom text field is the following one.

{% if status == '1' %}
  <div class="apply_job"><a href="/appy-job" target="_blank">Apply</a></div>
{% endif %}

screenshot

(I left the Status field visible just to check the link was only shown for published nodes.)

I first tried the following code, which didn't work.

{% if status == 1 %}
  <div class="apply_job"><a href="/appy-job" target="_blank">Apply</a></div>
{% endif %}

screenshot

I then noticed this error in the Drupal log, which made me understand that the code I entered was converting status to an integer.

Notice: Object of class Drupal\Core\Render\Markup could not be converted to int in __TwigTemplate_0e6762666bfbb9311bd9151c5ea0be83db756ef416475732470b4e0d9f6604e6->doDisplay() (line 32 of /var/www/html.original/sites/default/files/php/twig/61222d7b5a923___string_template__3f0751_iIyQMmqa7AXU9cjNTkh8j_Ed3/jVctGpgldXY6pQdNOz4-EYc-iwQbJzqU8T1VgO6g32E.php)

Notice that also the following code wouldn't work.

{% if  status  %}
  <div class="apply_job"><a href="/appy-job" target="_blank">Apply</a></div>
{% endif %}

It would show the link also for unpublished nodes.

screenshot

As side note, if the view is only shown to users who don't have access to unpublished nodes, or to whom information about unpublished nodes should not be shown, adding Published (=Yes) to the filter criteria is what I would do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.