-
Notifications
You must be signed in to change notification settings - Fork 16
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
WIP: First sketch of the OG context module. #252
Changes from all commits
03c0969
b267014
bd4bbcd
b17e50f
ed2f4a2
f4439c6
2ca93c8
273dd20
9f5d251
55e06bb
bc82a45
1666774
27c3fac
b263d46
a2f5eb7
70da07b
6e0b1e5
c00439b
245cc8a
ddb9d7f
ec3f732
cb4bf5a
3a6d73e
1753887
603fffe
113b402
f3c98a8
0a0f5e4
5aea9ee
becdcf7
9fce221
5ee68e9
f59e521
23a59c7
e9c7f0a
e7cad2d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
og.group_resolver_negotiation.*: | ||
type: config_entity | ||
label: 'OG group resolver negotiation config' | ||
mapping: | ||
id: | ||
type: string | ||
label: 'ID' | ||
label: | ||
type: label | ||
label: 'Label' | ||
description: | ||
type: string | ||
label: 'Description' | ||
status: | ||
type: boolean | ||
label: 'Status' | ||
weight: | ||
type: integer | ||
label: 'Weight' | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -74,3 +74,10 @@ function og_schema() { | |
|
||
return $schema; | ||
} | ||
|
||
/** | ||
* Implements hook_install(). | ||
*/ | ||
function og_install() { | ||
\Drupal\og\Og::groupResolverHandler()->updateConfigStorage(); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This shouldn't be needed. If I look at other modules that provide configurable plugins (e.g. block and views) then they also don't initialize their plugins on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shouldn't be needed, as the plugins should not be configurable. The only configuration is their weight and status (enabled/ disabled) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -164,6 +164,13 @@ function og_entity_create_access(AccountInterface $account, array $context, $bun | |
return $required ? AccessResult::forbiddenIf($node_access_strict) : AccessResult::neutral(); | ||
} | ||
|
||
/** | ||
* Implements hook_cache_flush(). | ||
*/ | ||
function og_cache_flush() { | ||
Og::groupResolverHandler()->updateConfigStorage(); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why would we need to update our config on cache clear? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think probably the only case when we would need to update our config is when a new module that provides a plugin is enabled. So probably we can better do this on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think shouldn't be needed at at all, since plugins won't have own config |
||
|
||
/** | ||
* Implements hook_entity_bundle_field_info(). | ||
* | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace Drupal\og\Annotation; | ||
|
||
use Drupal\Component\Annotation\Plugin; | ||
|
||
/** | ||
* Defines a OG group resolver item annotation object. | ||
* | ||
* @see \Drupal\og\Plugin\GroupResolverManager | ||
* @see plugin_api | ||
* | ||
* @Annotation | ||
*/ | ||
class GroupResolver extends Plugin { | ||
|
||
/** | ||
* The plugin ID. | ||
* | ||
* @var string | ||
*/ | ||
public $id; | ||
|
||
/** | ||
* The label of the plugin. | ||
* | ||
* @var \Drupal\Core\Annotation\Translation | ||
* | ||
* @ingroup plugin_translatable | ||
*/ | ||
public $label; | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
namespace Drupal\og\Entity; | ||
|
||
/** | ||
* @file | ||
* Contains \Drupal\og\Entity\GroupResolverNegotiation. | ||
*/ | ||
|
||
use Drupal\Core\Config\Entity\ConfigEntityBase; | ||
use Drupal\og\GroupResolverNegotiationInterface; | ||
|
||
/** | ||
* Defines the OG group resolver negotiation entity. | ||
* | ||
* @ConfigEntityType( | ||
* id = "group_resolver_negotiation", | ||
* label = @Translation("OG group resolver negotiation"), | ||
* config_prefix = "group_resolver_negotiation", | ||
* admin_permission = "administer site configuration", | ||
* entity_keys = { | ||
* "id" = "id", | ||
* "label" = "label", | ||
* "uuid" = "uuid" | ||
* } | ||
* ) | ||
*/ | ||
class GroupResolverNegotiation extends ConfigEntityBase implements GroupResolverNegotiationInterface { | ||
|
||
/** | ||
* The OG group resolver negotiation ID. | ||
* | ||
* @var string | ||
*/ | ||
protected $id; | ||
|
||
/** | ||
* The OG group resolver negotiation label. | ||
* | ||
* @var string | ||
*/ | ||
protected $label; | ||
|
||
/** | ||
* The status of the entity. | ||
* | ||
* @var boolean | ||
*/ | ||
protected $status; | ||
|
||
/** | ||
* The description of the plugin. | ||
* | ||
* @var string | ||
*/ | ||
protected $description; | ||
|
||
/** | ||
* The weight of the plugin. | ||
* | ||
* @var integer | ||
*/ | ||
protected $weight; | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
<?php | ||
|
||
namespace Drupal\og; | ||
|
||
/** | ||
* @file | ||
* Contains \Drupal\og\GroupResolverHandler. | ||
*/ | ||
|
||
use Drupal\Core\Config\ConfigFactoryInterface; | ||
use Drupal\Core\Entity\EntityTypeManagerInterface; | ||
|
||
/** | ||
* Class GroupResolverHandler. | ||
* | ||
* @package Drupal\og | ||
*/ | ||
class GroupResolverHandler implements GroupResolverHandlerInterface { | ||
|
||
/** | ||
* The config factory. | ||
* | ||
* @var \Drupal\Core\Config\ConfigFactoryInterface | ||
*/ | ||
protected $configFactory; | ||
|
||
/** | ||
* The OG group resolver manager. | ||
* | ||
* @var \Drupal\og\GroupResolverManager | ||
*/ | ||
protected $pluginManager; | ||
|
||
/** | ||
* The entity manager. | ||
* | ||
* @var \Drupal\Core\Entity\EntityStorageInterface | ||
*/ | ||
protected $storage; | ||
|
||
/** | ||
* Constructs an group resolver service. | ||
* | ||
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory | ||
* The config factory. | ||
* @param \Drupal\og\GroupResolverManager $context_manager | ||
* The OG context manager. | ||
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_manager | ||
* The entity type manager. | ||
*/ | ||
public function __construct(ConfigFactoryInterface $config_factory, GroupResolverManager $context_manager, EntityTypeManagerInterface $entity_manager) { | ||
$this->configFactory = $config_factory; | ||
$this->pluginManager = $context_manager; | ||
$this->storage = $entity_manager->getStorage('group_resolver_negotiation'); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getGroup() { | ||
$plugins = $this->getPlugins(); | ||
|
||
$groups = []; | ||
|
||
foreach ($plugins as $plugin) { | ||
if ($group = $this->getPlugin($plugin['id'])->getGroup()) { | ||
$groups = array_merge($groups, [$group]); | ||
} | ||
} | ||
|
||
// Return the first group for now. handle in a follow up PR to find the best | ||
// matching group. | ||
return reset($groups); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getPlugins($return_mode = GroupResolverHandlerInterface::RETURN_ONLY_ACTIVE) { | ||
|
||
/** @var \Drupal\og\Entity\GroupResolverNegotiation[] $group_resolver_config */ | ||
$group_resolver_config = $this->storage->loadMultiple(); | ||
|
||
$plugins = $this->pluginManager->getDefinitions(); | ||
|
||
if ($return_mode != GroupResolverHandlerInterface::RETURN_ALL) { | ||
|
||
foreach ($group_resolver_config as $context) { | ||
if ($return_mode == GroupResolverHandlerInterface::RETURN_ONLY_ACTIVE) { | ||
$condition = $context->get('status') == FALSE; | ||
} | ||
else { | ||
$condition = !in_array($context->id(), array_keys($plugins)); | ||
} | ||
|
||
if ($condition) { | ||
unset($plugins[$context->id()]); | ||
} | ||
} | ||
} | ||
|
||
if (!empty($group_resolver_config)) { | ||
uasort($plugins, function ($a, $b) use ($group_resolver_config) { | ||
return $group_resolver_config[$a['id']]->get('weight') > $group_resolver_config[$b['id']]->get('weight') ? 1 : -1; | ||
}); | ||
} | ||
|
||
return $plugins; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getPlugin($plugin_id) { | ||
return $this->pluginManager->createInstance($plugin_id); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function updatePlugin($plugin_id, $config = []) { | ||
/** @var GroupResolverNegotiation $contex */ | ||
$context = $this->storage->load($plugin_id); | ||
|
||
foreach ($config as $key => $value) { | ||
$context->set($key, $value); | ||
} | ||
|
||
$context->save(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function updateConfigStorage() { | ||
$plugins = $this->getPlugins(GroupResolverHandlerInterface::RETURN_ALL); | ||
|
||
$group_resolver_storage = $this->storage; | ||
$group_resolver_config = $group_resolver_storage->loadMultiple(); | ||
|
||
$weight = 0; | ||
foreach ($plugins as $plugin) { | ||
if (in_array($plugin['id'], array_keys($group_resolver_config))) { | ||
// The negotiation plugin already registered. | ||
continue; | ||
} | ||
|
||
// Registering a new negotiation plugin. | ||
$group_resolver_storage->create([ | ||
'id' => $plugin['id'], | ||
'label' => $plugin['label'], | ||
'description' => $plugin['description'], | ||
'status' => FALSE, | ||
'weight' => $weight, | ||
])->save(); | ||
|
||
$weight++; | ||
} | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the intention to save a separate config instance for each plugin? Do you foresee that we will need to save multiple instances of the same plugin? I can imagine for example the query argument plugin to have a configurable keyword, and you might want to save multiple instances of it, so you can derive the group if the URL has the query argument
?og_group_ref=groupid
as well as?group=groupid
.To me this sound overkill though. I think it would be simpler to have every plugin available once, and manage them solely with their on/off status and their weight. That means we can save the entire negotiation config in simple config. You would ignore the disabled plugins, and simply list the enabled ones, ordered by weight.
Example config file:
config/install/og.group_resolvers.yml
This would make it a lot easier to manage.