0

On the basic page content type, we have a paragraph field that is set to Plain Text with Link field and below are the paragraph type's fields.

How to append a <a> to the field so that it is displayed right at the end of the field?

Fields:

  1. Description (type = text (formatted, long))
  2. Link (type = link)

Current

Twig template:

{{ content.field_description }}
{{ content.field_link }}

Sample output:

<p>Dramatically create 24/7 paradigms via ubiquitous information. </p>
<a href="some/path/">Read More</a>

Goal

Twig Template:

{{ content.field_description }}

Desired Output:

<p>Dramatically create 24/7 paradigms via ubiquitous information. <a href="some/path/">Read More</a></p>

What we tried and got stuck. When we tried to print the $pr_description field using ksm(), we could not find a method or array where we could append the link to:

mytheme.theme

function mytheme_preprocess_paragraph__plain_text(&$variables) {
  $pr_plain = $variables['elements']['#paragraph'];
  if(!$pr_plain->get('field_link')->isEmpty()) {
    $pr_link_field = $pr_plain->get('field_link')->first();
    $pr_link_uri = $pr_link_field->uri;
    $pr_link_title = $pr_link_field->title;

    $pr_description = $pr_plain->get('field_description');
    ksm($pr_description); // this did not provide method or array such as `#suffix`
  }
}
1

You are using "Text formatted" for your description field, so the <p> tag is most likely part of the field value (an editor also could enter <table><tr><td>Dramatically create 24/7 paradigms via ubiquitous information. </td></tr></table> in that field). It will be much easier to achieve your desired output if you change the description field to plaintext or unformatted long.

  • the content editors need the ability to add bullet points, bold, italic, underline, and other features which is why we, unfortunately, had to do a text formatted field. – usernameabc Jul 12 '18 at 15:33
  • I fear there is no technically clean solution for this, as 1) the closing tag is part of the field value and not of the template code and 2) the closing tag is unknown (could be a </p> or an </ul>). If it has to be a formatted text field, I would scrap the link field and train your editors to include the link inside the text field. – Hudri Jul 13 '18 at 8:47
  • that is the other approach we offered so hopefully they can use that instead. It is just surprising that i cannot find a way to append the <a> using some method or array like #suffix. – usernameabc Jul 13 '18 at 18:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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