Add "profile_route_context" context provider

Created on 2 February 2023, about 2 years ago

Problem/Motivation

We have public profile pages and I've created a block that wants to pull in the profile context for particular imagery to appear.

Steps to reproduce

Try adding a ContextDefinition to a block for profile and it does not exist:

"profile" = @ContextDefinition("entity:profile", required = FALSE, label = @Translation("Profile")),

Proposed resolution

Add a context provider for profile based on the route like nodes/taxonomy terms do.

Remaining tasks

- Add the context provider
- Add block placement tests

📌 Task
Status

Active

Version

1.0

Component

Code

Created by

🇬🇧United Kingdom littlepixiez

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

Comments & Activities

  • Issue created by @littlepixiez
  • @littlepixiez opened merge request.
  • Assigned to littlepixiez
  • Status changed to Needs work about 2 years ago
  • 🇬🇧United Kingdom littlepixiez

    I need to add tests for this, but I've done the initial commit that provides the context provider.

  • 🇫🇷France blink38

    This patch seems correct. I wrote the same code in my own custom module I needed for another module displaying block in profile page.

    I have no idea on how to help writing test classes.

    I'm using the following code based on PHP attributes to use the context in my BlockBase.

    #[Block(
        id: "custom_user_block",
        admin_label: new TranslatableMarkup("Custom user block"),
        context_definitions: [
            "profile" => new EntityContextDefinition(
                data_type: 'entity:profile',
                label: 'Profile Context',
                required: FALSE
            )
        ]
    )]
    

    And in the build() :

    $profile = $this->getContextValue('profile');
    
    if ($profile->hasField('field_name')){
         $value = $profile->get('field_name')->value;
         ...
    }
    
Production build 0.71.5 2024