1

I'm working with an entity-creation form that needs some extensive theming. So far this has required me to alter template suggestions and create (twig) template variables for fieldsets. I've used template_preprocess_fieldset() and hook_theme_suggestions_fieldset_alter() to do this.

The suggestions and variables I'm creating are specific to one entity type, so I'd like to use the form id or the entity type to limit the action of the functions above, but the variables available within the scope of the functions don't include information outside the scope of the fieldsets themselves--in other words, I can't find out what form the fieldsets belong to.

Is there a way, in the context of a theme's preprocess_HOOK or HOOK_alter functions, to get information about the parent form?


I'd neglected to mention that I'd unsuccessfully tried @Berdir's two-part solution:

  1. Set some value in hook_form_alter()
  2. Access that value in hook_template_suggestions_fieldset_alter()

Going back at @Berdir's suggestion, I cracked it (thanks!) In Drupal 8, this combination works for my specific issue (and should work for other implementations of `hook_theme_suggestions_HOOK_alter() as well):

/**
 * Implements hook_form_alter().
 */
function themename_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  switch ($form_id) {
    case 'some_specific_form_id':
      foreach ($form as $key => $value) {
        if (strpos($key, 'field_') === 0) {
          $form[$key]['widget']['#parent_form_id'] = $form_id;
        }
      }
      break;
  }
}

/**
 * Implements hook_theme_suggestions_HOOK_alter().
 */
function u2d8_theme_suggestions_fieldset_alter(array &$suggestions, array $variables) {
  // Key the use of this suggestion to the front page.
  if ($variables['element']['#parent_form_id'] === 'some_specific_form_id') {
    $suggestions[] = 'fieldset__some_specific_form';
  }
}
2

Not directly, but you can do it in two steps. In a form alter, set whatever information you need on the element where you later need it.

I'd suggest do use some sort of prefix, like #project_form_id = ABC

That should then be available in the preprocess/suggestions hooks.

  • Thanks, I should have noted that I tried this earlier. But it looks like fields re rendered first and then forms--because anything I add to the fields is missing in e.g. the hook_theme_suggestions_fieldset_alter() implementation. – bhotel Aug 18 '16 at 16:07
  • Nope, that's definitely not the case. form alter runs first, how could you otherwise do things like change labels or completely remove elements if they're already rendered? If that's what you tried then you should share the relevant code and I might be able to point out why it didn't work – Berdir Aug 18 '16 at 17:08
  • Duh. You're right, obviously--accepted your answer, thanks! I had picked the wrong bit of the array to add the property to. In D8, you actually get helpful log errors if you do this: User error: "#parent_form_id" is an invalid render array key in Drupal\Core\Render\Element::children(). – bhotel Aug 19 '16 at 12:53
0

You can use [hook_field_widget_WIDGET_TYPE_form_alter][1] if you want to alter an widget on a certain form_id. One arguments passed through this hook is $form_state and $context , I think you can find form_id using them.

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.