Generic Entity Field Value Plugin

Created on 1 November 2019, about 5 years ago
Updated 19 June 2024, 5 months ago

Problem/Motivation

We had a project where customer requested to be able to select dynamically different layouts for different site elements like pages, headers, footers, separate blocks, paragraphs etc.
One of the requirements was to configure selected layout based on different entity fields including taxonomies, different entity types like nodes, taxonomy terms etc

Proposed resolution

We implemented a new multistep condition plugin which allows you to select a field from plugins entity context, click continue & select a value by using field's widget.

We deleted old plugin in the patch just to not create several plugins, also we decided to share this solution if it will be interesting for you & hopefully you can use it some way or integrate in existing module.

Also we implemented layout library module patch which have some implementation to support multistep ajax condition plugins & this plugin is basically used by that interfaces which are based on ctools ManageConditions ajax popup forms you can try it on your own: https://www.drupal.org/project/layout_library/issues/3089493 โœจ Library Layouts Auto Selection & Selection Rules Like It Was In Panels (D7) for Page Variants Needs work

Remaining tasks

Field value in configuration is stored like : [{value: 'dasda'}] (default result from $entity->get('name')->getValue() & in summary it has this string, as we have many different field types & their formatters probably it's a good idea to render it a more human friendly format

Sometime entity fields results in evaluation have additional garbage fields like _loaded, _accessCachablity etc, we get rid of it during plugin evalute probably some fields will need additional checks, we tested with taxonomies, entity references & basic properties like status etc.

User interface changes

Multistep form with back submit on the second step, select entity (context one) field, then using widget select value, save

โœจ Feature request
Status

Needs review

Version

1.0

Component

Code

Created by

๐Ÿ‡ฌ๐Ÿ‡ทGreece Pavel Ruban

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States SocialNicheGuru

    From #25 changed status

  • First commit to issue fork.
  • @codebymikey opened merge request.
  • Rerolled existing patch targeting the latest dev (and supporting Drupal 10).

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States webdrips

    The patch from #31 still has the same issue I reported in #25 unfortunately.

    Here is the full stack trace:

    Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException: You have requested a non-existent service "entity_field_condition.subform.helper_factory". in Drupal\Component\DependencyInjection\Container->get() (line 157 of core/lib/Drupal/Component/DependencyInjection/Container.php).
    
    Drupal\entity_field_condition\Plugin\Condition\FieldValue::create() (Line: 21)
    Drupal\Core\Plugin\Factory\ContainerFactory->createInstance() (Line: 59)
    Drupal\Core\Condition\ConditionManager->createInstance() (Line: 72)
    Drupal\layout_builder\EventSubscriber\SectionComponentVisibility->onBuildRender()
    call_user_func() (Line: 142)
    Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch() (Line: 117)
    Drupal\layout_builder\SectionComponent->toRenderArray() (Line: 88)
    Drupal\layout_builder\Section->toRenderArray() (Line: 316)
    Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay->buildSections() (Line: 275)
    Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay->buildMultiple() (Line: 340)
    Drupal\Core\Entity\EntityViewBuilder->buildComponents() (Line: 24)
    Drupal\node\NodeViewBuilder->buildComponents() (Line: 282)
    Drupal\Core\Entity\EntityViewBuilder->buildMultiple() (Line: 239)
    Drupal\Core\Entity\EntityViewBuilder->build()
    call_user_func_array() (Line: 101)
    Drupal\Core\Render\Renderer->doTrustedCallback() (Line: 788)
    Drupal\Core\Render\Renderer->doCallback() (Line: 374)
    Drupal\Core\Render\Renderer->doRender() (Line: 204)
    Drupal\Core\Render\Renderer->render() (Line: 242)
    Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent\{closure}() (Line: 580)
    Drupal\Core\Render\Renderer->executeInRenderContext() (Line: 243)
    Drupal\Core\Render\MainContent\HtmlRenderer->prepare() (Line: 132)
    Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse() (Line: 90)
    Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray()
    call_user_func() (Line: 142)
    Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch() (Line: 174)
    Symfony\Component\HttpKernel\HttpKernel->handleRaw() (Line: 81)
    Symfony\Component\HttpKernel\HttpKernel->handle() (Line: 58)
    Drupal\Core\StackMiddleware\Session->handle() (Line: 48)
    Drupal\Core\StackMiddleware\KernelPreHandle->handle() (Line: 106)
    Drupal\page_cache\StackMiddleware\PageCache->pass() (Line: 85)
    Drupal\page_cache\StackMiddleware\PageCache->handle() (Line: 50)
    Drupal\ban\BanMiddleware->handle() (Line: 48)
    Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle() (Line: 51)
    Drupal\Core\StackMiddleware\NegotiationMiddleware->handle() (Line: 23)
    Stack\StackedHttpKernel->handle() (Line: 718)
    Drupal\Core\DrupalKernel->handle() (Line: 19)
    

    Now using Drupal 9.5.9 and the EFC 8.1.4

    I didn't need to do anything to get this error this time except view a page on the site.

    We are using this in conjunction with the Layout Builder to show various views/fields based on field conditions (e.g. a taxonomy value).

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia PunamShelke India
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia PunamShelke India

    This is for D9.5 and php 8.1

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia PunamShelke India
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States webdrips

    Patch #35 kicks out an error:

    ParseError: syntax error, unexpected identifier "parent", expecting ";" in Composer\Autoload\includeFile() (line 443 of modules/contrib/entity_field_condition/src/Plugin/Condition/FieldValue.php).
    
  • Just an FYI, regarding #25, the reason Layout Builder context integration broke in D10 is referenced in #2916876-192: Add visibility control conditions to blocks within Layout Builder โ†’

  • ๐Ÿ‡จ๐Ÿ‡ฆCanada liquidcms

    Anyone know the status of this?

    We had it working on D9.5 but it does not work for D10. I see the patch has code which references: \Drupal\entity_field_condition\Subform\SubformHelperFactoryInterface but this doesnt exist in the 1.x or 2.x branches.

  • ๐Ÿ‡จ๐Ÿ‡ฆCanada liquidcms

    Not sure what #35 patch is as it refers to code which doesn't exist. To get this working for D10 i took the version i was using in D9 (1.3) and the patch i was using from here (#19) and simply made it D10 compatible by removing the composer.json file and setting core_version_requirement.

    Not quite the proper path forward i suspect but unclear the direction here.

  • ๐Ÿ‡จ๐Ÿ‡ฆCanada joseph.olstad

    joseph.olstad โ†’ made their first commit to this issueโ€™s fork.

  • @josepholstad opened merge request.
  • ๐Ÿ‡จ๐Ÿ‡ฆCanada joseph.olstad

    Ok I scrapped my own merge request.

    Try this patch instead

  • Status changed to Needs review about 1 year ago
  • ๐Ÿ‡จ๐Ÿ‡ฆCanada joseph.olstad

    patch 43 removes the garbage .orig file and I tried to do a better job dealing with conflicts than patch #35

    With that said, I am still reviewing the patch

  • ๐Ÿ‡จ๐Ÿ‡ฆCanada joseph.olstad

    Patch 43 resolves the issues that I noticed with patch #35

    Please review

  • ๐Ÿ‡จ๐Ÿ‡ฆCanada joseph.olstad

    Thanks to @liquidcms for reporting the success with patch #19 on Drupal 10 using a modified version of release 1.3.

    I took this as proof that patch #19 works well.

    So I re-rolled patch #19 on top of 8.x-1.x head of dev to create #43. The conflicts were carefully resolved and I believe the re-roll is a path forward here.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States webdrips

    #43 (and #19) no longer seem to work for me on Drupal 10 sites (10.1.5 or 10.1.6). I can't edit an existing field condition for visibility controls nor add a new one.

    In both cases, I get "error: context "entity" is missing." with an "Update" button that does nothing and a "Back" link.

    Steps to reproduce on an existing block with an existing rule:

    1. Click the context menu and select "control visibility"
    2. Click Edit
    3. "error: context "entity" is missing."

    On a new block or one without a visibility condition:

    1. Add the block to the layout builder
    2. Context menu > control visibility
    3. Select Field Value and click Add condition
    4. "error: context "entity" is missing."

    The field in question is a list (text) field.

  • Updated the default "Node Field (without preview)" condition so it's a bit more robust - such that the AJAX callbacks now works regardless of where it's used from e.g. Layout Builder and Blocks.

    Also added a new equals and regex operation, making the contains operation now more of a str_contains() check instead of a regex.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States kevinquillen

    @codebymikey does the patch in #48 fix the issue here as well?

    https://www.drupal.org/project/entity_field_condition/issues/3278442#com... ๐Ÿ› fields of bundle are not loaded when using Context UI Needs review

  • ๐Ÿ‡จ๐Ÿ‡ฆCanada darkodev

    I tested MR5 in the following forms and it works. Thanks very much for getting this far with this!

    Layout Builder (my original need for this patch along with the block visibility patches in https://www.drupal.org/project/drupal/issues/2916876 โœจ Add visibility control conditions to blocks within Layout Builder Needs work ).
    Works as expected, but throws an AJAX error after choosing the node type. In spite of the console error, it does load the fields and allows saving of the condition, which then works as expected.

    Core Block Layout
    Works as expected.

    Menu Position
    This module used to ship with a field value option in D7, which no longer exists in D10.
    Works as expected.

    I do not use Context module.

Production build 0.71.5 2024