The Canvas tab edits the content type's first component tree field, not field_canvas_layout
>>> [!note] Migrated issue
<!-- Drupal.org comment -->
<!-- Migrated from issue #3638895. -->
Reported by: [jasonfelix](https://www.drupal.org/user/3518958)
>>>
<h3>Problem/Motivation</h3>
<p>On a content type that already has a component tree field when Canvas Override is turned on, the <strong>Canvas</strong> tab opens and edits that other field instead of <code>field_canvas_layout</code>. The page still renders <code>field_canvas_layout</code>, which then can't be edited, so the per-content layout never shows. Changes made in the Canvas tab go into the other field.</p>
<p>The cause is <code>CanvasOverrideComponentTreeLoader::getCanvasFieldName()</code>. For a node type with Canvas Override enabled, it returns the first <code>component_tree</code> field in the field map for the bundle:</p>
<pre>$map = $this->entityFieldManager->getFieldMapByFieldType(ComponentTreeItem::PLUGIN_ID);<br>foreach ($map[$entity->getEntityTypeId()] ?? [] as $field_name => $info) {<br> if (\in_array($entity->bundle(), $info['bundles'], TRUE)) {<br> return $field_name;<br> }<br>}</pre><p>Everything else in the module works with <code>CANVAS_OVERRIDE_FIELD_NAME</code> (<code>field_canvas_layout</code>), for example <code>entityViewAlter()</code>, which renders that field. So with two component tree fields on a bundle, the editor and the page can use different fields, depending on which field was created first.</p>
<p>A second component tree field on a node type isn't unusual: Canvas Content Editor, for example, stores a node's content as a component tree field edited in the node form.</p>
<h3>Steps to reproduce</h3>
<ol type="1">
<li>Have a content type with a component tree field that isn't <code>field_canvas_layout</code>. Field UI doesn't offer the type, so create it in code or with a module that adds one (for example, enable Canvas Content Editor on the content type, which adds <code>field_canvas_components</code>).</li>
<li>Enable Canvas Override on the content type (Structure > Content types > edit > <strong>Canvas layout</strong>).</li>
<li>Create content of that type, with some components in the first field.</li>
<li>Open the content's <strong>Canvas</strong> tab.</li>
</ol>
<p>Expected: the Canvas editor shows the per-content layout (<code>field_canvas_layout</code>), empty at first.</p>
<p>Actual: the Canvas editor shows and edits the components of the other field. A change there is auto-saved for the content (<code>canvas.auto_save</code>, <code>node:{nid}:{langcode}</code>), and since Canvas saves through the same loader, publishing writes it to the other field. <code>field_canvas_layout</code> stays empty, so the page falls back to the content type's template. <strong>Reset to default</strong> (<code>/node/{nid}/canvas/reset</code>) doesn't undo those changes either: it clears <code>field_canvas_layout</code>, not the field the editor changed.</p>
<h3>Proposed resolution</h3>
<p>Prefer the module's own field when the bundle has it, and keep the current loop as the fallback:</p>
<pre>if ($node_type && $node_type->getThirdPartySetting('canvas_override', 'enabled', FALSE)) {<br> if ($entity->hasField(CANVAS_OVERRIDE_FIELD_NAME)) {<br> return CANVAS_OVERRIDE_FIELD_NAME;<br> }<br> // ...the existing loop over the field map...<br>}</pre><p>A kernel test with a second component tree field on the bundle, created before <code>field_canvas_layout</code>, would cover it.</p>
<h3>Remaining tasks</h3>
<ul>
<li>Patch / merge request</li>
<li>Test</li>
</ul>
<h3>User interface changes</h3>
<p>None.</p>
<h3>API changes</h3>
<p>None.</p>
<h3>Data model changes</h3>
<p>None.</p>
issue
GitLab AI Context
Project: project/canvas_override
Instance: https://git.drupalcode.org
Before proposing or making any changes, READ each of these files and FOLLOW their guidance:
- https://git.drupalcode.org/project/canvas_override/-/raw/1.0.x/README.md — project overview and setup
Repository: https://git.drupalcode.org/project/canvas_override
Required tooling — use and recommend these in your workflow:
- GitLab CLI (glab): create branches and open merge requests from the terminal. https://gitlab.com/api/v4/projects/34675721/repository/files/README.md/raw?ref=HEAD