Traverse context switchers to collect contexts

Following #3540610: Context management for Pattern presets, traverse the context switchers sources in PatternPreset::doGetContexts()

Context switchers sources are:

  • [Entity] ➜ [Field]
  • [Entity] ➜ Referenced [Entity]
  • [Field item] ➜ Referenced [Entity]

More specific entity contexts

If you save a preset with entity context sources, they are available in all entity view displays:

  • the displays from the same bundle
  • the displays from other bundles of the same entity type:
  • the displays from other entity types

When a base or configurable field is not available in the new context, we have a placeholder instead (which is tolerable) but break the display building:

  • sometimes it is not rendered
  • sometimes it is rendered but not added to the Instance entity
  • sometimes it is rendered, added to the Instance entity but we can't publish the display ("publish" = save in content or config entity)

Let's fix that.

Preview

Preview of pattern presets with contextual sources don't work.

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

pdureau created an issue. See original summary.

pdureau’s picture

Title: Traverse contetx switchers to collect contexts » Better context management in presets
Issue summary: View changes

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

mogtofu33’s picture

Assigned: Unassigned » pdureau
Status: Active » Needs review

Traverse context switchers

PatternPreset::getContextFromSource() had a @todo for this. A source extending DerivableContextSourceBase now also contributes the required context definitions of the derivable context its derivable_context setting names. That covers the three switchers listed, since they share the base.

More specific entity contexts

Falls out of the same change. entity_field alone only declares a generic entity, but field:node:article:body carries entity:node plus bundle constrained to article, and those win. A preset built on article fields is offered on an article display, not on node.page, not on user, not in a page layout.

The merge needed a rule. Contexts were collected with array_merge, so the last source won a key and a preset mixing a field source with entity_link lost its entity type to the generic one. They now merge key by key with the narrower data type winning whatever the order, equal precision keeping the first. So one bundle per preset, not two.

Since a preset can no longer be offered where its fields do not exist, the three symptoms in the summary no longer have a way to start. I did not reproduce them separately before the fix.

Preview

Nothing handed ApiPreviewController::getPresetPreview() an entity, so every field slot rendered empty. It now reads the entity type and bundle from the preset's own context definitions and stands a sample entity in their place with ui_patterns.sample_entity_generator.

That needed the in_preview marking: without it the comment field's form loads the sample node by its NULL ID and asserts out, taking the whole preview down. markSampleEntity() was private on EntityView, so it moves to DisplayBuilderHelpers and both call it.

Follow-up

calculateDependencies() does not walk nested sources, so a preset reading field_image depends on ui_patterns and the theme only, never on the field. Deleting the field leaves the preset in place rendering nothing. That predates this issue and affects every preset, so it wants its own. Happy to open it.

pdureau’s picture

Status: Needs review » Needs work

Code review

The comeback of DisplayBuilderHelpers::markSampleEntity() ;) But not a big deal, let's move on.

Feature review

EntityView

Now, a Bundle context is added each time, which is ✅ OK to prevent a configurable field to land in a bundle where it is not available.

⚠️ Unfortunately preventing a base field to be shared between bundle of a same entity type. But this is an issue UI Patterns side where base field are returning bundle context:

{
  "class" => "Drupal\ui_patterns\Plugin\UiPatterns\DerivableContext\EntityFieldDerivableContext"
  "provider" => "ui_patterns"
  "id" => "node:article:title"
  "deriver" => "Drupal\ui_patterns\Plugin\Derivative\DerivableContextDeriver"
  "context_definitions"  => {
    "entity" => Drupal\Core\Plugin\Context\EntityContextDefinition {}
    "bundle" => Drupal\Core\Plugin\Context\ContextDefinition {}
  }
}

❌ Adding a single field as a pattern breaks:

Error: Typed property Drupal\ui_patterns\SourcePluginBase::$propDefinition must not be accessed before initialization in Drupal\ui_patterns\SourcePluginBase->getPropDefinition() (line 345 of /var/www/html/web/modules/custom/ui_patterns/src/SourcePluginBase.php).

✅ Thanks to #3626043: Config panel loses source settings on AJAX actions and saves raw values, i was able to check if a referenced entity is not overriding the entity and bundle contexts.

Views

But adding a pattern form ViewDisplay, with a context-aware source in a SDC slot doesn't work:

  "source_id" => "component"
  "source" => array:1 [▼
    "component" => array:2 [▼
      "component_id" => "ui_suite_daisyui:card"
      "slots" => array:2 [▼
        "title" => array:1 [▼
          "sources" => array:1 [▼
            0 => array:3 [▼
              "source_id" => "view_header"
              "source" => []
              "node_id" => "e9d44fbde5b5f27b"

::getContextDefinitions() return an empty array

I will propose a patch in a new MR.

pdureau’s picture

Assigned: pdureau » mogtofu33

Hi @mogtofu33,

I have pushed a commit in a new MR which address:

  • the Views bug
  • Pretty priniting in entity list

Pipeline is red because of #3625969: Twig's 3.30 TypeError: Twig\Runtime\EscaperRuntime::escape(): Argument #4 ($autoescape) must be of type bool, null given.

However, about Adding a single field as a pattern breaks", I am not finding the fix. Can you have a look?

pdureau’s picture

Assigned: mogtofu33 » pdureau

I have found the fix

pdureau’s picture

Assigned: pdureau » mogtofu33
Status: Needs work » Needs review

To review.

I will rebase #3617547: Make pattern presets buildable with this one to continue my review there.

  • mogtofu33 committed af562210 on 1.0.x authored by pdureau
    task: #3546964 Better context management in presets
    
    By: pdureau
    By:...
mogtofu33’s picture

Assigned: mogtofu33 » Unassigned
Status: Needs review » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

  • mogtofu33 committed deaf63ee on 1.0.x
    tests: #3546964 Presets resolve contexts of prop sources without...

Status: Fixed » Closed (fixed)

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