Skip to content
Snippets Groups Projects
Commit 2fb03f35 authored by Kristiaan Van den Eynde's avatar Kristiaan Van den Eynde Committed by Kristiaan Van den Eynde
Browse files

Issue #3427552 by kristiaanvandeneynde: Switch to php attributes for plugin discovery

parent 134ba8f0
No related branches found
No related tags found
2 merge requests!193Issue #3304728 by kristiaanvandeneynde: Add member page missing due to...,!111Issue #3397021 by adamfranco: Add entity_mappings for phpstan-drupal
Pipeline #119066 passed with warnings
Showing
with 159 additions and 91 deletions
......@@ -3,22 +3,23 @@
namespace Drupal\gnode\Plugin\Group\Relation;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\group\Plugin\Attribute\GroupRelationType;
use Drupal\group\Plugin\Group\Relation\GroupRelationBase;
/**
* Provides a group relation type for nodes.
*
* @GroupRelationType(
* id = "group_node",
* label = @Translation("Group node"),
* description = @Translation("Adds nodes to groups both publicly and privately."),
* entity_type_id = "node",
* entity_access = TRUE,
* reference_label = @Translation("Title"),
* reference_description = @Translation("The title of the node to add to the group"),
* deriver = "Drupal\gnode\Plugin\Group\Relation\GroupNodeDeriver",
* )
*/
#[GroupRelationType(
id: 'group_node',
entity_type_id: 'node',
label: new TranslatableMarkup('Group node'),
description: new TranslatableMarkup('Adds nodes to groups both publicly and privately.'),
reference_label: new TranslatableMarkup('Title'),
reference_description: new TranslatableMarkup('The title of the node to add to the group'),
entity_access: TRUE,
deriver: 'Drupal\gnode\Plugin\Group\Relation\GroupNodeDeriver'
)]
class GroupNode extends GroupRelationBase {
/**
......
......@@ -4,7 +4,8 @@ namespace Drupal\group;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\DependencyInjection\ServiceProviderBase;
use Drupal\Core\Plugin\Discovery\AnnotatedClassDiscovery;
use Drupal\Core\Plugin\Discovery\AttributeDiscoveryWithAnnotations;
use Drupal\group\Plugin\Attribute\GroupRelationType;
use Drupal\group\Plugin\Group\Relation\GroupRelationTypeInterface;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
......@@ -28,9 +29,10 @@ class GroupServiceProvider extends ServiceProviderBase {
// Automatically create missing handler services for group relations.
$modules = $container->getParameter('container.modules');
$discovery = new AnnotatedClassDiscovery(
$discovery = new AttributeDiscoveryWithAnnotations(
'Plugin/Group/Relation',
$container->get('container.namespaces'),
GroupRelationType::class,
'Drupal\group\Annotation\GroupRelationType',
[]
);
......
<?php
namespace Drupal\group\Plugin\Attribute;
use Drupal\Component\Plugin\Attribute\Plugin;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\group\Plugin\Group\Relation\GroupRelationType as GroupRelationTypeDefinition;
/**
* Defines a group relation type for plugin discovery.
*
* Group relation type plugins use an object-based annotation method, rather
* than an array-type (as commonly used on other plugin types).
*
* The attribute properties of group relation types are found on
* \Drupal\group\Plugin\Group\Relation\GroupRelationType and are accessed
* using get/set methods defined in
* \Drupal\group\Plugin\Group\Relation\GroupRelationTypeInterface.
*/
#[\Attribute(\Attribute::TARGET_CLASS)]
class GroupRelationType extends Plugin {
public function __construct(
public readonly string $id,
public readonly string $entity_type_id,
public readonly TranslatableMarkup $label,
public readonly TranslatableMarkup $description,
public readonly ?TranslatableMarkup $reference_label = NULL,
public readonly ?TranslatableMarkup $reference_description = NULL,
public readonly string|false $entity_bundle = FALSE,
public readonly string|false $shared_bundle_class = FALSE,
public readonly bool $entity_access = FALSE,
public readonly string|false $admin_permission = FALSE,
public readonly string $pretty_path_key = 'content',
public readonly bool $enforced = FALSE,
public readonly bool $code_only = FALSE,
public readonly ?string $deriver = NULL,
public readonly array $additional = [],
) {}
/**
* {@inheritdoc}
*/
public function get(): array|object {
$to_filter = get_object_vars($this) + [
'class' => $this->getClass(),
'provider' => $this->getProvider(),
];
$values = array_filter($to_filter, function ($value, $key) {
return !($value === NULL && ($key === 'deriver' || $key === 'provider'));
}, ARRAY_FILTER_USE_BOTH);
return new GroupRelationTypeDefinition($values);
}
}
......@@ -3,23 +3,24 @@
namespace Drupal\group\Plugin\Group\Relation;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\group\Plugin\Attribute\GroupRelationType;
/**
* Provides a group relation for users as members.
*
* @GroupRelationType(
* id = "group_membership",
* label = @Translation("Group membership"),
* description = @Translation("Adds users to groups as members."),
* entity_type_id = "user",
* shared_bundle_class = "Drupal\group\Entity\GroupMembership",
* pretty_path_key = "member",
* reference_label = @Translation("User"),
* reference_description = @Translation("The user you want to make a member"),
* enforced = TRUE,
* admin_permission = "administer members"
* )
*/
#[GroupRelationType(
id: 'group_membership',
entity_type_id: 'user',
label: new TranslatableMarkup('Group membership'),
description: new TranslatableMarkup('Adds users to groups as members.'),
reference_label: new TranslatableMarkup('User'),
reference_description: new TranslatableMarkup('The user you want to make a member'),
shared_bundle_class: 'Drupal\group\Entity\GroupMembership',
admin_permission: 'administer members',
pretty_path_key: 'member',
enforced: TRUE
)]
class GroupMembership extends GroupRelationBase {
/**
......
......@@ -10,6 +10,7 @@ use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\group\Entity\GroupRelationshipTypeInterface;
use Drupal\group\Entity\GroupTypeInterface;
use Drupal\group\Plugin\Attribute\GroupRelationType;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
......@@ -112,7 +113,7 @@ class GroupRelationTypeManager extends DefaultPluginManager implements GroupRela
* The entity type manager.
*/
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct('Plugin/Group/Relation', $namespaces, $module_handler, 'Drupal\group\Plugin\Group\Relation\GroupRelationInterface', 'Drupal\group\Annotation\GroupRelationType');
parent::__construct('Plugin/Group/Relation', $namespaces, $module_handler, 'Drupal\group\Plugin\Group\Relation\GroupRelationInterface', GroupRelationType::class, 'Drupal\group\Annotation\GroupRelationType');
$this->alterInfo('group_relation_type');
$this->setCacheBackend($cache_backend, 'group_relations');
$this->entityTypeManager = $entity_type_manager;
......
......@@ -2,22 +2,23 @@
namespace Drupal\group_test_plugin\Plugin\Group\Relation;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\group\Plugin\Attribute\GroupRelationType;
use Drupal\group\Plugin\Group\Relation\GroupRelationBase;
/**
* Provides a group relation type for test entities.
*
* @GroupRelationType(
* id = "entity_test_as_content",
* label = @Translation("Group test entity"),
* description = @Translation("Adds test entities to groups."),
* entity_type_id = "entity_test_with_owner",
* entity_access = TRUE,
* pretty_path_key = "entity_test_with_owner",
* reference_label = @Translation("Test entity"),
* reference_description = @Translation("The name of the test entity you want to add to the group"),
* admin_permission = "administer entity_test_as_content"
* )
*/
#[GroupRelationType(
id: 'entity_test_as_content',
entity_type_id: 'entity_test_with_owner',
label: new TranslatableMarkup('Group test entity'),
description: new TranslatableMarkup('Adds test entities to groups.'),
reference_label: new TranslatableMarkup('Test entity'),
reference_description: new TranslatableMarkup('The name of the test entity you want to add to the group'),
entity_access: TRUE,
admin_permission: 'administer entity_test_as_content',
pretty_path_key: 'entity_test_with_owner'
)]
class EntityTestAsContent extends GroupRelationBase {
}
......@@ -2,21 +2,22 @@
namespace Drupal\group_test_plugin\Plugin\Group\Relation;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\group\Plugin\Attribute\GroupRelationType;
use Drupal\group\Plugin\Group\Relation\GroupRelationBase;
/**
* Provides a group relation type for groups.
*
* @GroupRelationType(
* id = "group_as_content",
* label = @Translation("Subgroup"),
* description = @Translation("Adds groups to groups as subgroups."),
* entity_type_id = "group",
* entity_bundle = "default",
* pretty_path_key = "subgroup",
* reference_label = @Translation("Group name"),
* reference_description = @Translation("The name of the group you want to add to the group")
* )
*/
#[GroupRelationType(
id: 'group_as_content',
entity_type_id: 'group',
label: new TranslatableMarkup('Subgroup'),
description: new TranslatableMarkup('Adds groups to groups as subgroups.'),
reference_label: new TranslatableMarkup('Group name'),
reference_description: new TranslatableMarkup('The name of the group you want to add to the group'),
entity_bundle: 'default',
pretty_path_key: 'subgroup'
)]
class GroupAsContent extends GroupRelationBase {
}
......@@ -2,19 +2,20 @@
namespace Drupal\group_test_plugin\Plugin\Group\Relation;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\group\Plugin\Attribute\GroupRelationType;
use Drupal\group\Plugin\Group\Relation\GroupRelationBase;
/**
* Provides a group relation type for nodes.
*
* @GroupRelationType(
* id = "node_as_content",
* label = @Translation("Node as content"),
* description = @Translation("Adds nodes to groups."),
* entity_type_id = "node",
* entity_access = TRUE,
* deriver = "Drupal\group_test_plugin\Plugin\Group\Relation\NodeAsContentDeriver",
* )
*/
#[GroupRelationType(
id: 'node_as_content',
entity_type_id: 'node',
label: new TranslatableMarkup('Node as content'),
description: new TranslatableMarkup('Adds nodes to groups.'),
entity_access: TRUE,
deriver: 'Drupal\group_test_plugin\Plugin\Group\Relation\NodeAsContentDeriver'
)]
class NodeAsContent extends GroupRelationBase {
}
......@@ -2,19 +2,20 @@
namespace Drupal\group_test_plugin\Plugin\Group\Relation;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\group\Plugin\Attribute\GroupRelationType;
use Drupal\group\Plugin\Group\Relation\GroupRelationBase;
/**
* Provides a group relation type for node types.
*
* @GroupRelationType(
* id = "node_type_as_content",
* label = @Translation("Node type as content"),
* description = @Translation("Adds node types to groups."),
* entity_type_id = "node_type",
* entity_access = TRUE,
* admin_permission = "administer node_type_as_content",
* )
*/
#[GroupRelationType(
id: 'node_type_as_content',
entity_type_id: 'node_type',
label: new TranslatableMarkup('Node type as content'),
description: new TranslatableMarkup('Adds node types to groups.'),
entity_access: TRUE,
admin_permission: 'administer node_type_as_content'
)]
class NodeTypeAsContent extends GroupRelationBase {
}
......@@ -2,21 +2,22 @@
namespace Drupal\group_test_plugin\Plugin\Group\Relation;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\group\Plugin\Attribute\GroupRelationType;
use Drupal\group\Plugin\Group\Relation\GroupRelationBase;
/**
* Provides a group relation type for users.
*
* @GroupRelationType(
* id = "user_as_content",
* label = @Translation("Group user"),
* description = @Translation("Adds users to groups without making them members."),
* entity_type_id = "user",
* pretty_path_key = "user",
* reference_label = @Translation("Username"),
* reference_description = @Translation("The name of the user you want to add to the group"),
* admin_permission = "administer user_as_content"
* )
*/
#[GroupRelationType(
id: 'user_as_content',
entity_type_id: 'user',
label: new TranslatableMarkup('Group user'),
description: new TranslatableMarkup('Adds users to groups without making them members.'),
reference_label: new TranslatableMarkup('Username'),
reference_description: new TranslatableMarkup('The name of the user you want to add to the group'),
admin_permission: 'administer user_as_content',
pretty_path_key: 'user'
)]
class UserAsContent extends GroupRelationBase {
}
......@@ -2,22 +2,23 @@
namespace Drupal\group_test_plugin\Plugin\Group\Relation;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\group\Plugin\Attribute\GroupRelationType;
use Drupal\group\Plugin\Group\Relation\GroupRelationBase;
/**
* Provides a group relation type for users.
*
* @GroupRelationType(
* id = "user_as_content_shared_bundle_class",
* label = @Translation("Group user"),
* description = @Translation("Relates users to groups without making them members."),
* entity_type_id = "user",
* pretty_path_key = "user_shared_bundle_class",
* shared_bundle_class = "Drupal\group_test_plugin\Entity\GroupedUser",
* reference_label = @Translation("Username"),
* reference_description = @Translation("The name of the user you want to relate to the group"),
* admin_permission = "administer user_as_content_shared_bundle_class"
* )
*/
#[GroupRelationType(
id: 'user_as_content_shared_bundle_class',
entity_type_id: 'user',
label: new TranslatableMarkup('Group user'),
description: new TranslatableMarkup('Relates users to groups without making them members.'),
reference_label: new TranslatableMarkup('Username'),
reference_description: new TranslatableMarkup('The name of the user you want to relate to the group'),
shared_bundle_class: 'Drupal\group_test_plugin\Entity\GroupedUser',
admin_permission: 'administer user_as_content_shared_bundle_class',
pretty_path_key: 'user_shared_bundle_class'
)]
class UserAsContentSharedBundleClass extends GroupRelationBase {
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment