Score:2

Why can't some fields be moved within details element?

us flag

I am trying to move 2 form fields to the advanced sidebar: dismissible and status. Status was already part of the meta container and setting #group='meta' on dismissable also placed it there; but these weren't in the proper order. Nothing I do seems to have any impact on:

  • dismissible
  • standard meta fields: published, author, revision log
  • status

Both dismissible and status are fields added by the Sitewide Alert module which defines the entity which this is the edit form for.

I gave up trying to move my 2 fields (together) so I created my own details block as:

  $form['alert_settings'] = [
    '#type' => 'details',
    '#group' => 'advanced',
    '#weight' => -99,  
    '#title' => t('Alert settings'),
    '#tree' => TRUE,
    '#access' => TRUE,
    '#open' => TRUE,
  ];

and then assigned both fields #group='alert_settings'. This works fine as it creates a new details group which is placed at the top of the right sidebar. Perfect, except I want status to be the top item in this block, followed by dismissible.

No matter which of the status #weight options I set (or dismissable ones); I can't change the order of these.

I have tried:

  $form['status']['#weight'] = -99;
  $form['status']['widget']['#weight'] = -99;
  $form['status']['widget']['value']['#weight'] = -99;

What am I missing here?

Score:2
cn flag

It seems ultimately to be down to this code in EntityFormDisplay::processForm():

// Assign the weights configured in the form display.
foreach ($this->getComponents() as $name => $options) {
  if (isset($element[$name])) {
    $element[$name]['#weight'] = $options['weight'];
  }
}

This method is a #process callback for the form which runs after form alter hooks, so your changes are lost.

You could add your own process callback and change the weights there, but it might be a bit cleaner to alter it at the component level with hook_entity_form_display_alter(), e.g.:

function MODULE_entity_form_display_alter(\Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display, array $context) {
  if ($context['entity_type'] == 'sitewide_alert') {
    $status = $form_display->getComponent('status');
    // 'dismissible' is set to the base field's weight, -10.
    $status['weight'] = -11;
    $form_display->setComponent('status', $status);
  }
}

Incidentally, $form['status']['#weight'] would have been the right choice if the process callback wasn't overriding it.

liquidcms avatar
us flag
This does work. Thanks for this. Have never used that hook before but willcertainly keep it in mind as I have come across this issue a few times now where sidebar elements we not movable.
cn flag
No problem, I tracked down where the issue comes from and updated the answer in case you’re interested. Seems to be at least somewhat “by design”
liquidcms avatar
us flag
Thanks for the added info. Also not surprising that the module has coded something to force something odd. It took me a while to figure out why i couldn't make the entity fieldable when someone pointed out there was code in the module to explicitly block making the entity fieldable (for no good reason).
cn flag
I saw that question too, spent 20mins of confused poking around the code before 4k4 thankfully worked it out and brought some sanity. I would never have thought to look for what turned out to be the problem either. Funnily enough the problem for _this_ question isn't that module, the weight override code is in core.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.