WorkspaceSubscriber service parameter $workspaceAssociation must be optional

Created on 29 December 2023, 11 months ago
Updated 2 May 2024, 7 months ago

Problem/Motivation

In the service definition content_moderation.services.yml we have

content_moderation.workspace_subscriber:
  class: Drupal\content_moderation\EventSubscriber\WorkspaceSubscriber
  arguments: ['@entity_type.manager', '@?workspaces.association']
  tags:
    - { name: event_subscriber }

Notice that @?workspaces.association is optional.

In the class itself \Drupal\content_moderation\EventSubscriber\WorkspaceSubscriber we have

/**
 * Constructs a new WorkspaceSubscriber instance.
 *
 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
 *   The entity type manager service.
 * @param \Drupal\workspaces\WorkspaceAssociationInterface $workspaceAssociation
 *   The workspace association service.
 */
public function __construct(
  protected readonly EntityTypeManagerInterface $entityTypeManager,
  protected readonly WorkspaceAssociationInterface $workspaceAssociation
) {}

Notice that $workspaceAssociation is required.

Proposed resolution

Make $workspaceAssociation optional in \Drupal\content_moderation\EventSubscriber\WorkspaceSubscriber.

Remaining tasks

Implement the proposed resolution.

User interface changes

Nope.

API changes

Nope.

Data model changes

Nope.

Release notes snippet

Nope.

๐Ÿ“Œ Task
Status

Fixed

Version

10.3 โœจ

Component
Content moderationย  โ†’

Last updated about 11 hours ago

  • Maintained by
  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia @Sam152
Created by

๐Ÿ‡ณ๐Ÿ‡ฑNetherlands ndf Amsterdam

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.

  • Issue created by @ndf
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia keshav patel

    Keshav Patel โ†’ made their first commit to this issueโ€™s fork.

  • Status changed to Needs review 11 months ago
  • ๐Ÿ‡ณ๐Ÿ‡ฑNetherlands ndf Amsterdam
  • ๐Ÿ‡ณ๐Ÿ‡ฑNetherlands ndf Amsterdam

    @Keshev Patel, thanks for the MR. Can you please explain why you opted for the required parameter solution? This is helpful for other people to review your MR.

  • Status changed to Needs work 11 months ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    Yes an explanation is needed. Also a test case

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia pooja saraah Chennai

    In the original service definition, the second argument is specified as @?workspaces.association. The ? indicates that this argument is optional, meaning that the WorkspaceSubscriber class can accept either the workspaces.association service or no service at all.

    In the modified service definition, the optional operator ? has been removed from the second argument. This means that the WorkspaceSubscriber class now explicitly expects the @workspaces.association service as a required argument.

    Purpose of the Change:
    The modification aligns the service definition with the actual constructor requirements of the WorkspaceSubscriber class. By removing the optional operator, the service definition accurately reflects that the WorkspaceSubscriber class expects the @workspaces.association service to be provided during instantiation.

  • ๐Ÿ‡จ๐Ÿ‡ฆCanada dstorozhuk Chicago ๐Ÿ‡บ๐Ÿ‡ธ, Toronto ๐Ÿ‡จ๐Ÿ‡ฆ, Kyiv ๐Ÿ‡บ๐Ÿ‡ฆ

    After apply the patch from merge request !5978 I have that error.
    The service "content_moderation.workspace_subscriber" has a dependency on a non-existent service "workspaces.association".

  • ๐Ÿ‡ท๐Ÿ‡ดRomania amateescu

    In the service definition, the workspaces.association service must be marked as optional because it might not exist (for example when the Workspaces module is not enabled).

    Note that this is not a bug because \Drupal\content_moderation\EventSubscriber\WorkspaceSubscriber is an event subscriber class, and it shouldn't be instantiated unless it needs to react to the WorkspacePrePublishEvent event, which is only invoked when the Workspaces module is enabled. If some other module instantiates it.. well, it shouldn't :)

    If we want to make a change in core though, we need to mark the argument as optional in \Drupal\content_moderation\EventSubscriber\WorkspaceSubscriber::__construct(). Updated the issue summary to reflect that.

  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany stefan.korn Jossgrund

    stefan.korn โ†’ made their first commit to this issueโ€™s fork.

  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany stefan.korn Jossgrund

    Thanks for the explanation.

    As mentioned in ๐Ÿ› Workspaces can't be published from the command line Fixed devel quits with WSOD on "Event info". And yes, this is happening if the content moderation module is installed and the workspaces module is not.

    Basically

    Drupal::service('event_dispatcher')->getListeners();
    

    is failing then.

    It seems to me that getListeners() without event argument is a valid call, therefore I am wondering if this should really end in error here (no exceptions for the method pointing in that direction either) even if the workspaces module is not installed.

    And the difference between service definition (optional) and implementation (required) does also seem not really straightforward.

    I have amended the MR to make the change in the implementation and not the service defintion.

  • Status changed to RTBC 7 months ago
  • ๐Ÿ‡ท๐Ÿ‡ดRomania amateescu

    Ok, let's do this, the change is harmless and allows the class to be instantiated :)

    • catch โ†’ committed a60890d4 on 11.x
      Issue #3411308 by stefan.korn, ndf, amateescu: WorkspaceSubscriber...
    • catch โ†’ committed 0a15c5b8 on 10.3.x
      Issue #3411308 by stefan.korn, ndf, amateescu: WorkspaceSubscriber...
  • Status changed to Fixed 7 months ago
  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom catch

    Committed/pushed to 11.x and cherry-picked to 10.3.x, thanks!

  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany stefan.korn Jossgrund
Production build 0.71.5 2024