Overview

While working on #3502819: Connect "new page" with API to create new pages I noticed the published checkbox is always checked. Even though the entity is not.

Steps to reproduce:

  • For Page entity type:
    1. Create a new page using the "Add new" => "Page" inside the navigator
    2. The page is unpublished, but the toggle in the "Page data" column says it's published
  • For Node entity type:
    1. Go to /node/add/article, enter a title, and uncheck the "Published" checkbox
    2. Now edit it in the XB UI, and observe how the "Published" toggle is toggled on

Proposed resolution

Probably due to "0" string value. The value is status[value]:"0". See note I have in that issue's MR:

  // `status comes as a numeric string from the backend but is a boolean when modified in the editor.
  const published =
    entity_form_fields['status[value]'] === '1' ||
    entity_form_fields['status[value]'] === true;

User interface changes

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

mglaman created an issue. See original summary.

mglaman’s picture

Issue summary: View changes

roshni upadhyay made their first commit to this issue’s fork.

roshni upadhyay’s picture

Assigned: Unassigned » roshni upadhyay
roshni upadhyay’s picture

The issue is with the API that when a node is updated, the updated content is not reflected in the API response. Instead, the API continues to return the old data. This problem occurs because the following code retrieves data from the auto-save manager, which may not have the latest changes:

if ($body = $this->autoSaveManager->getAutoSaveData($entity)) {
    ['layout' => $layout, 'model' => $model, 'entity_form_fields' => $entity_form_fields] = $body;
}

roshni upadhyay changed the visibility of the branch 3503199-published-checkbox-is to hidden.

roshni upadhyay changed the visibility of the branch 3503199-published-checkbox-is to hidden.

roshni upadhyay changed the visibility of the branch 3503199-published-checkbox-is to active.

wim leers’s picture

Title: "Published" checkbox is always checked even if the entity is not » "Published" toggle is always toggled "on"
Component: Page builder » Semi-Coupled theme engine
Assigned: roshni upadhyay » Unassigned
StatusFileSize
new348.04 KB
new56.99 KB

Debugged this.

#5 is inaccurate AFAICT.

Debugging trail

  1. Create a fresh node with status: false. Say, node 4.
  2. Load it in Experience Builder at /xb/node/4/editor. Observe that the Published toggle is toggled on.
  3. Put a breakpoint in the code that generates this form: \Drupal\experience_builder\Controller\EntityFormController::form(). Observe this is the return value:

    Most importantly: #value === FALSE.
  4. In \Drupal\Core\Render\Element\Checkbox::preRenderCheckbox(), that's still the case.
  5. ⚠️ However, that maps the #return_value Form API property to the value attribute:
    Element::setAttributes($element, ['id', 'name', '#return_value' => 'value']);
    

    which results in

  6. Look at the corresponding FE code: /ui/src/components/form/components/drupal/DrupalToggle.tsx. It contains this:
      <Toggle
        checked={!!attributes?.value}
    

    … which appears accurate but isn't. The value attribute for <input type="checkbox"> is very interesting 🤪, see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/checkbox....

    So AFAICT the problem lies in DrupalToggle, which was introduced in #3491265: Split form components into `Drupal`-prefixed behavioral wrappers and presentational components.

Looking at /node/<nid>/edit

  • checked "published" checkbox: <input data-drupal-selector="edit-status-value" type="checkbox" id="edit-status-value" name="status[value]" value="1" class="form-checkbox form-boolean form-boolean--type-checkbox">
  • unchecked "published" checkbox:
    <input data-drupal-selector="edit-status-value" type="checkbox" id="edit-status-value" name="status[value]" value="1" checked="checked" class="form-checkbox form-boolean form-boolean--type-checkbox">
    

👆 That's the markup being generated outside of XB. That's the starting point for things we do on top.

Tentative conclusion: needs only front-end changes?

So AFAICT updating DrupalToggle should work? But

-    checked={!!attributes?.value}
+    checked={!!attributes?.checked}

didn't do the trick 😅

I'm getting lost between:

  1. ui/src/components/form/components/Checkbox.tsx
  2. ui/src/components/form/components/drupal/DrupalInput.tsx
  3. ui/src/components/form/components/Toggle.tsx
  4. ui/src/components/form/components/drupal/DrupalToggle.tsx

… because all four of those (!!!) are dealing with the checked attribute 😅 AFAICT only the last 2 are relevant. I changed both like indicated above, without success.

I've done the due diligence, and think it's now down to somebody who knows the Semi-Coupled theme engine well enough to finish it up. It probably takes them only minutes given the digging I've done so far 🤞

wim leers’s picture

Title: "Published" toggle is always toggled "on" » "Published" toggle is always toggled "on", even when `checked` attribute is absent
swarad07’s picture

-    checked={!!attributes?.value}
+    checked={!!attributes?.checked}

That should be the way, as the correct attribute to check should be checked and not value

However, the other issue seems to be that the attributes.checked is coming as true always.

I am guessing somewhere in the flow that the value parameter of 1 is getting associated with the checked HTML attribute as true.

Needs further digging.

wim leers’s picture

Title: "Published" toggle is always toggled "on", even when `checked` attribute is absent » `DrupalToggle` for `boolean_checkbox` widget is always toggled "on", even when `checked` attribute is absent
balintbrews’s picture

Status: Active » Postponed (maintainer needs more info)
Issue tags: +Needs issue summary update

I made an update to the DrupalToggle component in #3500017: Defining props for code components, then @jessebaker pointed me at this issue.

Can someone, please, help me with steps to reproduce? We have solid investigation bits by Wim in #10, but I'm having a hard time to see how to actually reproduce the problem — unless it went away. (At the time of writing this comment, #3500017: Defining props for code components is not in, but I don't think that will fix this anyway.)

lauriii’s picture

Issue summary: View changes
Status: Postponed (maintainer needs more info) » Active
Issue tags: -Needs issue summary update

@balintbrews Added steps to the IS 😊

lauriii’s picture

Issue summary: View changes
StatusFileSize
new1.02 MB

Adding a gif

wim leers’s picture

Issue summary: View changes

Added STR for using article nodes.

roshni upadhyay’s picture

I have made some changes in ui/src/components/form/components/inputBehaviors.tsx, which have resolved the checkbox issue. The autosave state is now updating correctly. However, still facing a problem when moving a node from published to draft.

jessebaker made their first commit to this issue’s fork.

jessebaker’s picture

Status: Active » Fixed

Thanks, merged!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.