0

For example to render a simple form like search block should be invoked 8 different templates like below and each of this template only has one or few line of code, see below:

region.html.twig
└─ block.html.twig
   └─ form.html.twig
      ├─ form-element.html.twig
      |  ├─ form-element-label.html.twig
      |  └─ input.html.twig
      └─ container.html.twig
          └─ input.html.twig

As you can see in below some of this files Just have a few line of code:
region.html.twig

{% if content %}
  <div{{ attributes }}>
    {{ content }}
  </div>
{% endif %}

form.html.twig

<form{{ attributes }}>
  {{ children }}
</form>

form-element.html.twig

{% if title is not empty or required -%}
  <label{{ attributes.addClass(classes) }}>{{ title }}</label>
{%- endif %}

input.html.twig

<input{{ attributes }} />{{ children }}

container.html.twig

<div{{ attributes.addClass(classes) }}>{{ children }}</div>
  • Some of this templates files are common with other parts of page and editing each of them can affect to other parts and cause unwanted changes . for example in this case drupal theme system for: "input.html.twig", suggest only input--submit.html.twig and this suggestion also common between search block and search form.
  • Editing and managing this template files is tedious and difficult and make my theme folder busy.
  • Most of these templates have ambiguity and unclear content code

For this reason I'm looking for a method to combine this template files and make one template file like "form--search-block.html.twig" to I can edit all element needs for rendering a form in one special place and with more flexibility and confidently.

4
  • Sure you can (I don't know why you'd want to do that though), you have to define your own custom preprocess in your template and remove any dependencies to the prior parent theme and build up your own twig file and name it whatever you want
    – Sohail
    Commented Mar 3, 2017 at 10:07
  • @Sohail: Thank's for your help and replay, Can you give me more explain or any reference? Commented Mar 3, 2017 at 10:28
  • Technically, yes. Practical? No. Also it doesn't take 8 templates to render the search form, it is a chain of things involved in rendering each piece of the page.
    – Kevin
    Commented Mar 3, 2017 at 14:22
  • @Kevin: Thank you so much, I'll be very happy if you share and present your technically solution. Commented Mar 3, 2017 at 14:44

1 Answer 1

2

In drupal 8 there's this rule called theme inheritance and backward compatibility, what you have to do to accomplish what you want is to make sure that your subtheme does not inherit any core theme's HOOKS

You might want to play around in your subtheme and set your base theme to null I haven't checked it myself but it might work.

And then of course you have to define new preprocess templating rules in your themename.theme file using theme_suggestions_page_alter function, you can read more here

Then based on what you defined in your .theme file you may proceed with creating a unified template file

Also you might want to read this suggestion that somewhat explains a similar use case

2
  • 1
    Thank you so much for your help and attention, I appreciate you a lot, But in this case I can't get any idea or feasible solution from those references, I'm looking for a practical solution. I will be glad to have more cooperation and become more familiar with you in future. Commented Mar 3, 2017 at 14:35
  • 1
    The thing is Mojtaba, what you want to do is so not useful that kind of breaks the whole concept of Drupal and the way it works, so people don't really ever tried to accomplish this before, so there's no real practical reference for it, unless you want to dig into it yourself which will take quite some time, anyhow if you ever tried to make this happen it would be nice to get an update from you, good luck
    – Sohail
    Commented Mar 4, 2017 at 8:38

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.