1

I would like to alter the branding block to add a textfield in the block form to specify the URL it should redirect to. The URL is currently fixed to <front> in the Twig template.

What I've done so far is to add the text field with hook_form_alter. I'm saving the value using $form['entity_builders'] and $block->setThirdPartySetting. This works.

I want to retrieve this value in hook_preprocess_block__system_branding_block(&$variables) but $variables does not contain the block entity. How can I get the block in this preprocess function so that I can load my added value with $block->getThirdPartySetting?

1 Answer 1

0

A different approach: You can override the system branding block template via the block--system-branding-block.html.twig file (instead of using a preprocess hook).

On a Bootstrap theme, block--system-branding-block.html.twig looks like this:

{% block content %}
  {% if site_logo %}
    <a class="logo navbar-btn pull-left" href="{{ path('<front>') }}" title="{{ 'Home'|t }}" rel="home">
      <img src="{{ site_logo }}" alt="{{ 'Home'|t }}" />
    </a>
  {% endif %}
  {% if site_name %}
    <a class="name navbar-brand" href="{{ path('<front>') }}" title="{{ 'Home'|t }}" rel="home">{{ site_name }}</a>
  {% endif %}
  {% if site_slogan %}
    <p class="navbar-text">{{ site_slogan }}</p>
  {% endif %}
{% endblock %}

You can alter the href of those links to point anywhere you'd like, for example:

<a class="logo navbar-btn pull-left" href="/some/other/page" title="{{ 'Home'|t }}" rel="home"><src="{{ site_logo }}" alt="{{ 'Home'|t }}" /></a>
2
  • I already alter the block theme file. But I need the url come from the new field added to the block config. I don't want to have it fixed in the twig file.
    – Michele
    Commented Oct 21, 2019 at 20:14
  • Oh, I see, sorry I missed that part of you question.
    – ymdahi
    Commented Oct 21, 2019 at 20:45

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.