Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PROD-30967: Hux implementation #4118

Merged
merged 6 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ public static function processFilterTags(array &$element, FormStateInterface $fo
/**
* Handles switching the available terms based on the selected vocabulary.
*/
public static function updateTagsOptions($form, FormStateInterface $form_state) {
public static function updateTagsOptions(array $form, FormStateInterface $form_state) {

// Check if there is triggered parent of element.
if ($triggered = $form_state->getTriggeringElement()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,7 @@ services:
arguments: []
tags:
- { name: cache.context }

Drupal\social_group\CurrentGroupService:
social_group.current_group_service: '@Drupal\social_group\CurrentGroupService'
autowire: true
52 changes: 52 additions & 0 deletions modules/social_features/social_group/src/CurrentGroupService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Drupal\social_group;

use Drupal\Core\Plugin\Context\ContextRepositoryInterface;
use Drupal\group\Entity\GroupInterface;

/**
* Centralized ways to get the current group.
*
* @package Drupal\social_group.
*/
class CurrentGroupService {

/**
* The context repository interface.
*
* @var \Drupal\Core\Plugin\Context\ContextRepositoryInterface
*/
private ContextRepositoryInterface $contextRepository;

/**
* Constructor.
*
* @param \Drupal\Core\Plugin\Context\ContextRepositoryInterface $context_repository
* The context repository.
*/
public function __construct(
ContextRepositoryInterface $context_repository,
) {
$this->contextRepository = $context_repository;
}

/**
* Get group from runtime contexts.
*
* @return \Drupal\group\Entity\GroupInterface|null
* The current group or NULL.
*/
public function fromRunTimeContexts(): ?GroupInterface {
denis-getopensocial marked this conversation as resolved.
Show resolved Hide resolved
$group_runtime_context = $this->contextRepository->getRuntimeContexts(['@group.group_route_context:group']);

$group = $group_runtime_context['@group.group_route_context:group']->getContextData()->getValue();
if ($group === NULL) {
return NULL;
}

assert($group instanceof GroupInterface, "The group context resolver returned a context value that is not a GroupInterface instance which violates the services contract.");
return $group;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

namespace Drupal\Tests\social_group\Unit;

use Drupal\Core\Plugin\Context\ContextInterface;
use Drupal\Core\Plugin\Context\ContextRepositoryInterface;
use Drupal\Core\TypedData\TypedDataInterface;
use Drupal\group\Entity\GroupInterface;
use Drupal\social_group\CurrentGroupService;
use Drupal\Tests\UnitTestCase;
use PHPUnit\Framework\MockObject\MockObject;

/**
* Unit test for CurrentGroupService.
*
* @group social_group
*/
class CurrentGroupServiceTest extends UnitTestCase {

/**
* The mocked context repository interface.
*
* @var \Drupal\Core\Plugin\Context\ContextRepositoryInterface|\PHPUnit\Framework\MockObject\MockObject
*/
protected ContextRepositoryInterface|MockObject $contextRepository;

/**
* The service under test.
*
* @var \Drupal\social_group\CurrentGroupService
*/
protected CurrentGroupService $currentGroupService;

/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->contextRepository = $this->createMock(ContextRepositoryInterface::class);

$this->currentGroupService = new CurrentGroupService($this->contextRepository);
}

/**
* Test that fromRunTimeContexts() returns a Group when a group is present.
*/
public function testFromRunTimeContextsWithGroup(): void {
$group = $this->createMock(GroupInterface::class);
$this->mockContext($group);

// Call the method and assert the group is returned.
$result = $this->currentGroupService->fromRunTimeContexts();
$this->assertSame($group, $result, 'Group context should return a GroupInterface instance.');
}

/**
* Test that fromRunTimeContexts() returns NULL when group is not present.
*/
public function testFromRunTimeContextsWithoutGroup(): void {
$this->mockContext(NULL);

// Call the method and assert NULL is returned.
$result = $this->currentGroupService->fromRunTimeContexts();
$this->assertNull($result, 'Group context should return NULL.');
}

/**
* Mock the context.
*
* @param \PHPUnit\Framework\MockObject\MockObject|\Drupal\group\Entity\GroupInterface|null $group
* The mocked group or NULL.
*/
private function mockContext(MockObject|GroupInterface|NULL $group): void {
$typedData = $this->createMock(TypedDataInterface::class);
$typedData->expects($this->once())
->method('getValue')
->willReturn($group);

$context = $this->createMock(ContextInterface::class);
$context->expects($this->once())
->method('getContextData')
->willReturn($typedData);

$this->contextRepository
->expects($this->once())
->method('getRuntimeContexts')
->with(['@group.group_route_context:group'])
->willReturn(['@group.group_route_context:group' => $context]);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\Core\Routing\CurrentRouteMatch;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\social_group\CurrentGroupService;
use Drupal\social_post\Plugin\Block\PostBlock;

/**
Expand All @@ -28,7 +30,9 @@ public function __construct(
EntityTypeManagerInterface $entity_type_manager,
AccountProxyInterface $current_user,
FormBuilderInterface $form_builder,
ModuleHandlerInterface $module_handler
ModuleHandlerInterface $module_handler,
CurrentRouteMatch $route_match,
CurrentGroupService $current_group_service,
) {
parent::__construct(
$configuration,
Expand All @@ -37,7 +41,9 @@ public function __construct(
$entity_type_manager,
$current_user,
$form_builder,
$module_handler
$module_handler,
$route_match,
$current_group_service,
);

// Override the bundle type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\Core\Routing\CurrentRouteMatch;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\social_group\CurrentGroupService;
use Drupal\social_post\Plugin\Block\PostGroupBlock;

/**
Expand All @@ -28,7 +30,9 @@ public function __construct(
EntityTypeManagerInterface $entity_type_manager,
AccountProxyInterface $current_user,
FormBuilderInterface $form_builder,
ModuleHandlerInterface $module_handler
ModuleHandlerInterface $module_handler,
CurrentRouteMatch $route_match,
CurrentGroupService $current_group_service,
) {
parent::__construct(
$configuration,
Expand All @@ -37,7 +41,9 @@ public function __construct(
$entity_type_manager,
$current_user,
$form_builder,
$module_handler
$module_handler,
$route_match,
$current_group_service,
);

// Override the bundle type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\Core\Routing\CurrentRouteMatch;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\social_group\CurrentGroupService;
use Drupal\social_post\Plugin\Block\PostProfileBlock;

/**
Expand All @@ -29,7 +31,8 @@ public function __construct(
AccountProxyInterface $current_user,
FormBuilderInterface $form_builder,
ModuleHandlerInterface $module_handler,
$account
CurrentRouteMatch $route_match,
CurrentGroupService $current_group_service,
) {
parent::__construct(
$configuration,
Expand All @@ -39,7 +42,8 @@ public function __construct(
$current_user,
$form_builder,
$module_handler,
$account
$route_match,
$current_group_service,
);

// Override the bundle type.
Expand Down
7 changes: 4 additions & 3 deletions modules/social_features/social_post/social_post.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ core_version_requirement: ^9 || ^10
dependencies:
- drupal:block
- drupal:comment
- social:dropdown
- drupal:field
- group:group
- drupal:options
- social:social_comment
- drupal:text
- drupal:user
- drupal:views
- group:group
- social:activity_creator
- social:dropdown
- social:social_comment
- social:social_group
package: Social
39 changes: 0 additions & 39 deletions modules/social_features/social_post/social_post.module
Original file line number Diff line number Diff line change
Expand Up @@ -15,45 +15,6 @@ use Drupal\group\Entity\GroupInterface;
use Drupal\group\Entity\Group;
use Drupal\user\UserInterface;

/**
denis-getopensocial marked this conversation as resolved.
Show resolved Hide resolved
* Implements hook_form_FORM_ID_alter().
*/
function social_post_form_post_form_alter(&$form, FormStateInterface $form_state, $form_id) {

if (
$form_state->getFormObject()->getEntity()->isNew() &&
($content = \Drupal::service('social_post.helper')->buildCurrentUserImage())
) {
$form['current_user_image'] = $content;
}

// Reset title display.
$form['field_post']['widget'][0]['#title_display'] = "";

// Set submit button caption to Post instead of Save.
$form['actions']['submit']['#value'] = t('Post', [], ['context' => 'Post button']);

if (!empty($form['field_post']) && !empty($form['field_post']['widget'][0])) {
// For posting on the stream on the group stream.
if (!empty(_social_group_get_current_group())) {
$form['field_post']['widget'][0]['#placeholder'] = t('Say something to the group');
$form['field_post']['widget'][0]['#title'] = t('Say something to the group');
}
// For the post on a users profile.
elseif (!empty(\Drupal::routeMatch()->getParameter('user')) && \Drupal::routeMatch()->getParameter('user')->id() != \Drupal::currentUser()->id()) {
$user_profile = \Drupal::routeMatch()->getParameter('user');
$name = $user_profile->getDisplayName();
$form['field_post']['widget'][0]['#placeholder'] = t('Leave a message to @name', ['@name' => $name]);
$form['field_post']['widget'][0]['#title'] = t('Leave a message to @name', ['@name' => $name]);
}
else {
$title = t('Say something to the Community');
$form['field_post']['widget'][0]['#title'] = $title;
$form['field_post']['widget'][0]['#placeholder'] = $title;
}
}
}

/**
* Implements hook_form_FORM_ID_alter().
*
Expand Down
14 changes: 14 additions & 0 deletions modules/social_features/social_post/social_post.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,17 @@ services:
social_post.helper:
class: Drupal\social_post\Service\SocialPostHelper
arguments: ['@entity_type.manager', '@current_user']

#hooks replacement
denis-getopensocial marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor Author

@denis-getopensocial denis-getopensocial Dec 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the proper auto-wiring should looks like this (credit Alexander)

#hooks replacement
# use autowiring for arguments for `SocialPostFormHooks`
social_post.form.hooks:
  class: Drupal\social_post\Hooks\SocialPostFormHooks
  autowire: true
  tags:
  - { name: hooks }
# make `SocialPostFormHooks` injectable in another service (e.g. for decoration)
Drupal\social_post\Hooks\SocialPostFormHooks: '@social_post.form.hooks'

social_post.form.hooks:
class: Drupal\social_post\Hooks\SocialPostFormHooks
arguments:
- '@social_post.helper'
- '@current_user'
denis-getopensocial marked this conversation as resolved.
Show resolved Hide resolved
tags:
- { name: hooks }

Drupal\social_post\Hooks\SocialPostFormHooks:
class: Drupal\social_post\Hooks\SocialPostFormHooks
autowire: true
social_post.form.hooks: '@Drupal\social_post\Hooks\SocialPostFormHooks'
Loading
Loading