From 03c09693941dc5d581aa120c4c7efb87c5fc1cf5 Mon Sep 17 00:00:00 2001 From: RoySegall Date: Sun, 17 Jul 2016 19:26:31 +0300 Subject: [PATCH 01/32] First sketch of the OG context module. --- modules/og_context/og_context.info.yml | 7 ++++ modules/og_context/og_context.services.yml | 4 +++ .../og_context/src/Annotation/OgContext.php | 34 +++++++++++++++++++ modules/og_context/src/OgContext.php | 25 ++++++++++++++ .../src/Plugin/OgContext/Enitty.php | 16 +++++++++ .../og_context/src/Plugin/OgContext/Url.php | 16 +++++++++ .../og_context/src/Plugin/OgContextBase.php | 12 +++++++ .../src/Plugin/OgContextInterface.php | 12 +++++++ .../src/Plugin/OgContextManager.php | 33 ++++++++++++++++++ 9 files changed, 159 insertions(+) create mode 100644 modules/og_context/og_context.info.yml create mode 100644 modules/og_context/og_context.services.yml create mode 100644 modules/og_context/src/Annotation/OgContext.php create mode 100644 modules/og_context/src/OgContext.php create mode 100644 modules/og_context/src/Plugin/OgContext/Enitty.php create mode 100644 modules/og_context/src/Plugin/OgContext/Url.php create mode 100644 modules/og_context/src/Plugin/OgContextBase.php create mode 100644 modules/og_context/src/Plugin/OgContextInterface.php create mode 100644 modules/og_context/src/Plugin/OgContextManager.php diff --git a/modules/og_context/og_context.info.yml b/modules/og_context/og_context.info.yml new file mode 100644 index 000000000..b5d49b30a --- /dev/null +++ b/modules/og_context/og_context.info.yml @@ -0,0 +1,7 @@ +name: OG context +type: module +description: Get a group from a viewed page. +core: 8.x +package: OG +dependencies: + - og diff --git a/modules/og_context/og_context.services.yml b/modules/og_context/og_context.services.yml new file mode 100644 index 000000000..8f491bc2f --- /dev/null +++ b/modules/og_context/og_context.services.yml @@ -0,0 +1,4 @@ +services: + plugin.manager.og.context: + class: Drupal\og_context\Plugin\OgContextManager + parent: default_plugin_manager diff --git a/modules/og_context/src/Annotation/OgContext.php b/modules/og_context/src/Annotation/OgContext.php new file mode 100644 index 000000000..df5df44f9 --- /dev/null +++ b/modules/og_context/src/Annotation/OgContext.php @@ -0,0 +1,34 @@ +createInstance($plugin); + } + +} diff --git a/modules/og_context/src/Plugin/OgContext/Enitty.php b/modules/og_context/src/Plugin/OgContext/Enitty.php new file mode 100644 index 000000000..ef27162b0 --- /dev/null +++ b/modules/og_context/src/Plugin/OgContext/Enitty.php @@ -0,0 +1,16 @@ +alterInfo('og_context_og_context_info'); + $this->setCacheBackend($cache_backend, 'og_context_og_context_plugins'); + } + +} From b26701486c594cdb8337209549ba57b2dc61c2e6 Mon Sep 17 00:00:00 2001 From: RoySegall Date: Tue, 19 Jul 2016 09:18:51 +0300 Subject: [PATCH 02/32] Adding files. --- .../install/og_context.ogcontextneogation.yml | 1 + .../schema/og_context_config.schema.yml | 12 ++++ modules/og_context/og_context.info.yml | 2 +- modules/og_context/og_context.links.menu.yml | 7 ++ .../og_context/src/Entity/OgContextConfig.php | 47 +++++++++++++ .../src/Entity/OgContextConfigInterface.php | 13 ++++ .../src/Form/OgContextConfigForm.php | 67 +++++++++++++++++++ .../src/Form/OgContextNeogation.php | 58 ++++++++++++++++ .../src/OgContextConfigHtmlRouteProvider.php | 59 ++++++++++++++++ .../src/OgContextConfigListBuilder.php | 32 +++++++++ 10 files changed, 297 insertions(+), 1 deletion(-) create mode 100644 modules/og_context/config/install/og_context.ogcontextneogation.yml create mode 100644 modules/og_context/config/schema/og_context_config.schema.yml create mode 100644 modules/og_context/og_context.links.menu.yml create mode 100644 modules/og_context/src/Entity/OgContextConfig.php create mode 100644 modules/og_context/src/Entity/OgContextConfigInterface.php create mode 100644 modules/og_context/src/Form/OgContextConfigForm.php create mode 100644 modules/og_context/src/Form/OgContextNeogation.php create mode 100644 modules/og_context/src/OgContextConfigHtmlRouteProvider.php create mode 100644 modules/og_context/src/OgContextConfigListBuilder.php diff --git a/modules/og_context/config/install/og_context.ogcontextneogation.yml b/modules/og_context/config/install/og_context.ogcontextneogation.yml new file mode 100644 index 000000000..52668545d --- /dev/null +++ b/modules/og_context/config/install/og_context.ogcontextneogation.yml @@ -0,0 +1 @@ +og_context: diff --git a/modules/og_context/config/schema/og_context_config.schema.yml b/modules/og_context/config/schema/og_context_config.schema.yml new file mode 100644 index 000000000..b263d7747 --- /dev/null +++ b/modules/og_context/config/schema/og_context_config.schema.yml @@ -0,0 +1,12 @@ +og_context.og_context_config.*: + type: config_entity + label: 'OG context config config' + mapping: + id: + type: string + label: 'ID' + label: + type: label + label: 'Label' + uuid: + type: string diff --git a/modules/og_context/og_context.info.yml b/modules/og_context/og_context.info.yml index b5d49b30a..53799ffce 100644 --- a/modules/og_context/og_context.info.yml +++ b/modules/og_context/og_context.info.yml @@ -4,4 +4,4 @@ description: Get a group from a viewed page. core: 8.x package: OG dependencies: - - og + - og_ui diff --git a/modules/og_context/og_context.links.menu.yml b/modules/og_context/og_context.links.menu.yml new file mode 100644 index 000000000..ebba9e3c4 --- /dev/null +++ b/modules/og_context/og_context.links.menu.yml @@ -0,0 +1,7 @@ +# OG context config menu items definition +entity.og_context_config.collection: + title: 'OG context' + route_name: entity.og_context_config.collection + description: 'Manage order of OG context plugins.' + parent: og_ui.admin_index + weight: 99 diff --git a/modules/og_context/src/Entity/OgContextConfig.php b/modules/og_context/src/Entity/OgContextConfig.php new file mode 100644 index 000000000..5d3fb13df --- /dev/null +++ b/modules/og_context/src/Entity/OgContextConfig.php @@ -0,0 +1,47 @@ +entity; + $form['label'] = array( + '#type' => 'textfield', + '#title' => $this->t('Label'), + '#maxlength' => 255, + '#default_value' => $og_context_config->label(), + '#description' => $this->t("Label for the OG context config."), + '#required' => TRUE, + ); + + $form['id'] = array( + '#type' => 'machine_name', + '#default_value' => $og_context_config->id(), + '#machine_name' => array( + 'exists' => '\Drupal\og_context\Entity\OgContextConfig::load', + ), + '#disabled' => !$og_context_config->isNew(), + ); + + /* You will need additional form elements for your custom properties. */ + + return $form; + } + + /** + * {@inheritdoc} + */ + public function save(array $form, FormStateInterface $form_state) { + $og_context_config = $this->entity; + $status = $og_context_config->save(); + + switch ($status) { + case SAVED_NEW: + drupal_set_message($this->t('Created the %label OG context config.', [ + '%label' => $og_context_config->label(), + ])); + break; + + default: + drupal_set_message($this->t('Saved the %label OG context config.', [ + '%label' => $og_context_config->label(), + ])); + } + $form_state->setRedirectUrl($og_context_config->urlInfo('collection')); + } + +} diff --git a/modules/og_context/src/Form/OgContextNeogation.php b/modules/og_context/src/Form/OgContextNeogation.php new file mode 100644 index 000000000..240f29c06 --- /dev/null +++ b/modules/og_context/src/Form/OgContextNeogation.php @@ -0,0 +1,58 @@ +entityManager = $entity_manager; + } + + public static function create(ContainerInterface $container) { + return new static( + $container->get('entity.manager') + ); + } + + + /** + * {@inheritdoc} + */ + public function getFormId() { + return 'og_context_neogation'; + } + + /** + * {@inheritdoc} + */ + public function buildForm(array $form, FormStateInterface $form_state) { + + return $form; + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + + } + +} diff --git a/modules/og_context/src/OgContextConfigHtmlRouteProvider.php b/modules/og_context/src/OgContextConfigHtmlRouteProvider.php new file mode 100644 index 000000000..9986468c1 --- /dev/null +++ b/modules/og_context/src/OgContextConfigHtmlRouteProvider.php @@ -0,0 +1,59 @@ +id(); + + if ($collection_route = $this->getCollectionRoute($entity_type)) { + $collection->add("entity.{$entity_type_id}.collection", $collection_route); + } + + return $collection; + } + + /** + * Gets the collection route. + * + * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type + * The entity type. + * + * @return \Symfony\Component\Routing\Route|null + * The generated route, if available. + */ + protected function getCollectionRoute(EntityTypeInterface $entity_type) { + if ($entity_type->hasLinkTemplate('collection') && $entity_type->hasListBuilderClass()) { + $entity_type_id = $entity_type->id(); + $route = new Route($entity_type->getLinkTemplate('collection')); + $route + ->setDefaults([ + '_entity_list' => $entity_type_id, + // Make sure this is not a TranslatableMarkup object as the + // TitleResolver translates this string again. + '_title' => (string) $entity_type->getLabel(), + ]) + ->setRequirement('_permission', $entity_type->getAdminPermission()) + ->setOption('_admin_route', TRUE); + + return $route; + } + } + +} diff --git a/modules/og_context/src/OgContextConfigListBuilder.php b/modules/og_context/src/OgContextConfigListBuilder.php new file mode 100644 index 000000000..d194d9085 --- /dev/null +++ b/modules/og_context/src/OgContextConfigListBuilder.php @@ -0,0 +1,32 @@ +t('OG context config'); + $header['id'] = $this->t('Machine name'); + return $header + parent::buildHeader(); + } + + /** + * {@inheritdoc} + */ + public function buildRow(EntityInterface $entity) { + $row['label'] = $entity->label(); + $row['id'] = $entity->id(); + // You probably want a few more properties here... + return $row + parent::buildRow($entity); + } + +} From bd4bbcd38eafef45c5636c5b6529c76c99e7be79 Mon Sep 17 00:00:00 2001 From: Roy Segall Date: Fri, 22 Jul 2016 11:36:12 +0300 Subject: [PATCH 03/32] Moving OG context to OG core. --- .../schema/og_context_negotiation.schema.yml | 4 +- .../install/og_context.ogcontextneogation.yml | 1 - modules/og_context/og_context.info.yml | 7 -- modules/og_context/og_context.links.menu.yml | 7 -- modules/og_context/og_context.services.yml | 4 -- .../og_context/src/Entity/OgContextConfig.php | 47 ------------- .../src/Entity/OgContextConfigInterface.php | 13 ---- .../src/Form/OgContextConfigForm.php | 67 ------------------- .../src/Form/OgContextNeogation.php | 58 ---------------- modules/og_context/src/OgContext.php | 25 ------- .../src/OgContextConfigHtmlRouteProvider.php | 59 ---------------- .../src/Plugin/OgContext/Enitty.php | 16 ----- og.services.yml | 3 + .../src => src}/Annotation/OgContext.php | 3 +- src/Entity/OgContextNegotiation.php | 57 ++++++++++++++++ src/Og.php | 15 +++++ .../src/Plugin => src}/OgContextBase.php | 2 +- .../src/Plugin => src}/OgContextInterface.php | 2 +- .../src/Plugin => src}/OgContextManager.php | 5 +- src/OgContextNegotiationInterface.php | 17 +++++ .../OgContextNegotiationListBuilder.php | 16 +++-- src/Plugin/OgContext/Entity.php | 16 +++++ .../src => src}/Plugin/OgContext/Url.php | 4 +- 23 files changed, 127 insertions(+), 321 deletions(-) rename modules/og_context/config/schema/og_context_config.schema.yml => config/schema/og_context_negotiation.schema.yml (69%) delete mode 100644 modules/og_context/config/install/og_context.ogcontextneogation.yml delete mode 100644 modules/og_context/og_context.info.yml delete mode 100644 modules/og_context/og_context.links.menu.yml delete mode 100644 modules/og_context/og_context.services.yml delete mode 100644 modules/og_context/src/Entity/OgContextConfig.php delete mode 100644 modules/og_context/src/Entity/OgContextConfigInterface.php delete mode 100644 modules/og_context/src/Form/OgContextConfigForm.php delete mode 100644 modules/og_context/src/Form/OgContextNeogation.php delete mode 100644 modules/og_context/src/OgContext.php delete mode 100644 modules/og_context/src/OgContextConfigHtmlRouteProvider.php delete mode 100644 modules/og_context/src/Plugin/OgContext/Enitty.php rename {modules/og_context/src => src}/Annotation/OgContext.php (91%) create mode 100644 src/Entity/OgContextNegotiation.php rename {modules/og_context/src/Plugin => src}/OgContextBase.php (83%) rename {modules/og_context/src/Plugin => src}/OgContextInterface.php (83%) rename {modules/og_context/src/Plugin => src}/OgContextManager.php (88%) create mode 100644 src/OgContextNegotiationInterface.php rename modules/og_context/src/OgContextConfigListBuilder.php => src/OgContextNegotiationListBuilder.php (60%) create mode 100644 src/Plugin/OgContext/Entity.php rename {modules/og_context/src => src}/Plugin/OgContext/Url.php (66%) diff --git a/modules/og_context/config/schema/og_context_config.schema.yml b/config/schema/og_context_negotiation.schema.yml similarity index 69% rename from modules/og_context/config/schema/og_context_config.schema.yml rename to config/schema/og_context_negotiation.schema.yml index b263d7747..0e5ddd163 100644 --- a/modules/og_context/config/schema/og_context_config.schema.yml +++ b/config/schema/og_context_negotiation.schema.yml @@ -1,6 +1,6 @@ -og_context.og_context_config.*: +og.og_context_negotiation.*: type: config_entity - label: 'OG context config config' + label: 'OG context negotiation config' mapping: id: type: string diff --git a/modules/og_context/config/install/og_context.ogcontextneogation.yml b/modules/og_context/config/install/og_context.ogcontextneogation.yml deleted file mode 100644 index 52668545d..000000000 --- a/modules/og_context/config/install/og_context.ogcontextneogation.yml +++ /dev/null @@ -1 +0,0 @@ -og_context: diff --git a/modules/og_context/og_context.info.yml b/modules/og_context/og_context.info.yml deleted file mode 100644 index 53799ffce..000000000 --- a/modules/og_context/og_context.info.yml +++ /dev/null @@ -1,7 +0,0 @@ -name: OG context -type: module -description: Get a group from a viewed page. -core: 8.x -package: OG -dependencies: - - og_ui diff --git a/modules/og_context/og_context.links.menu.yml b/modules/og_context/og_context.links.menu.yml deleted file mode 100644 index ebba9e3c4..000000000 --- a/modules/og_context/og_context.links.menu.yml +++ /dev/null @@ -1,7 +0,0 @@ -# OG context config menu items definition -entity.og_context_config.collection: - title: 'OG context' - route_name: entity.og_context_config.collection - description: 'Manage order of OG context plugins.' - parent: og_ui.admin_index - weight: 99 diff --git a/modules/og_context/og_context.services.yml b/modules/og_context/og_context.services.yml deleted file mode 100644 index 8f491bc2f..000000000 --- a/modules/og_context/og_context.services.yml +++ /dev/null @@ -1,4 +0,0 @@ -services: - plugin.manager.og.context: - class: Drupal\og_context\Plugin\OgContextManager - parent: default_plugin_manager diff --git a/modules/og_context/src/Entity/OgContextConfig.php b/modules/og_context/src/Entity/OgContextConfig.php deleted file mode 100644 index 5d3fb13df..000000000 --- a/modules/og_context/src/Entity/OgContextConfig.php +++ /dev/null @@ -1,47 +0,0 @@ -entity; - $form['label'] = array( - '#type' => 'textfield', - '#title' => $this->t('Label'), - '#maxlength' => 255, - '#default_value' => $og_context_config->label(), - '#description' => $this->t("Label for the OG context config."), - '#required' => TRUE, - ); - - $form['id'] = array( - '#type' => 'machine_name', - '#default_value' => $og_context_config->id(), - '#machine_name' => array( - 'exists' => '\Drupal\og_context\Entity\OgContextConfig::load', - ), - '#disabled' => !$og_context_config->isNew(), - ); - - /* You will need additional form elements for your custom properties. */ - - return $form; - } - - /** - * {@inheritdoc} - */ - public function save(array $form, FormStateInterface $form_state) { - $og_context_config = $this->entity; - $status = $og_context_config->save(); - - switch ($status) { - case SAVED_NEW: - drupal_set_message($this->t('Created the %label OG context config.', [ - '%label' => $og_context_config->label(), - ])); - break; - - default: - drupal_set_message($this->t('Saved the %label OG context config.', [ - '%label' => $og_context_config->label(), - ])); - } - $form_state->setRedirectUrl($og_context_config->urlInfo('collection')); - } - -} diff --git a/modules/og_context/src/Form/OgContextNeogation.php b/modules/og_context/src/Form/OgContextNeogation.php deleted file mode 100644 index 240f29c06..000000000 --- a/modules/og_context/src/Form/OgContextNeogation.php +++ /dev/null @@ -1,58 +0,0 @@ -entityManager = $entity_manager; - } - - public static function create(ContainerInterface $container) { - return new static( - $container->get('entity.manager') - ); - } - - - /** - * {@inheritdoc} - */ - public function getFormId() { - return 'og_context_neogation'; - } - - /** - * {@inheritdoc} - */ - public function buildForm(array $form, FormStateInterface $form_state) { - - return $form; - } - - /** - * {@inheritdoc} - */ - public function submitForm(array &$form, FormStateInterface $form_state) { - - } - -} diff --git a/modules/og_context/src/OgContext.php b/modules/og_context/src/OgContext.php deleted file mode 100644 index 5b4746fcd..000000000 --- a/modules/og_context/src/OgContext.php +++ /dev/null @@ -1,25 +0,0 @@ -createInstance($plugin); - } - -} diff --git a/modules/og_context/src/OgContextConfigHtmlRouteProvider.php b/modules/og_context/src/OgContextConfigHtmlRouteProvider.php deleted file mode 100644 index 9986468c1..000000000 --- a/modules/og_context/src/OgContextConfigHtmlRouteProvider.php +++ /dev/null @@ -1,59 +0,0 @@ -id(); - - if ($collection_route = $this->getCollectionRoute($entity_type)) { - $collection->add("entity.{$entity_type_id}.collection", $collection_route); - } - - return $collection; - } - - /** - * Gets the collection route. - * - * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type - * The entity type. - * - * @return \Symfony\Component\Routing\Route|null - * The generated route, if available. - */ - protected function getCollectionRoute(EntityTypeInterface $entity_type) { - if ($entity_type->hasLinkTemplate('collection') && $entity_type->hasListBuilderClass()) { - $entity_type_id = $entity_type->id(); - $route = new Route($entity_type->getLinkTemplate('collection')); - $route - ->setDefaults([ - '_entity_list' => $entity_type_id, - // Make sure this is not a TranslatableMarkup object as the - // TitleResolver translates this string again. - '_title' => (string) $entity_type->getLabel(), - ]) - ->setRequirement('_permission', $entity_type->getAdminPermission()) - ->setOption('_admin_route', TRUE); - - return $route; - } - } - -} diff --git a/modules/og_context/src/Plugin/OgContext/Enitty.php b/modules/og_context/src/Plugin/OgContext/Enitty.php deleted file mode 100644 index ef27162b0..000000000 --- a/modules/og_context/src/Plugin/OgContext/Enitty.php +++ /dev/null @@ -1,16 +0,0 @@ -createInstance($plugin); + } + /** * Invalidate cache. * diff --git a/modules/og_context/src/Plugin/OgContextBase.php b/src/OgContextBase.php similarity index 83% rename from modules/og_context/src/Plugin/OgContextBase.php rename to src/OgContextBase.php index 159bd0939..f99ed88e7 100644 --- a/modules/og_context/src/Plugin/OgContextBase.php +++ b/src/OgContextBase.php @@ -1,6 +1,6 @@ alterInfo('og_context_og_context_info'); $this->setCacheBackend($cache_backend, 'og_context_og_context_plugins'); diff --git a/src/OgContextNegotiationInterface.php b/src/OgContextNegotiationInterface.php new file mode 100644 index 000000000..ff8260460 --- /dev/null +++ b/src/OgContextNegotiationInterface.php @@ -0,0 +1,17 @@ +t('OG context config'); + $header['label'] = $this->t('OG context negotiation'); $header['id'] = $this->t('Machine name'); return $header + parent::buildHeader(); } @@ -23,7 +27,7 @@ public function buildHeader() { * {@inheritdoc} */ public function buildRow(EntityInterface $entity) { - $row['label'] = $entity->label(); + $row['label'] = $this->getLabel($entity); $row['id'] = $entity->id(); // You probably want a few more properties here... return $row + parent::buildRow($entity); diff --git a/src/Plugin/OgContext/Entity.php b/src/Plugin/OgContext/Entity.php new file mode 100644 index 000000000..67458ca26 --- /dev/null +++ b/src/Plugin/OgContext/Entity.php @@ -0,0 +1,16 @@ + Date: Fri, 22 Jul 2016 13:10:55 +0300 Subject: [PATCH 04/32] Commit more work. --- og.services.yml | 5 ++++- src/Og.php | 15 ++++----------- src/OgContextBase.php | 2 +- src/OgContextHandler.php | 26 ++++++++++++++++++++++++++ src/OgContextHandlerInterface.php | 7 +++++++ src/OgContextInterface.php | 11 ++++++++++- src/OgContextManager.php | 4 ++-- src/Plugin/OgContext/Entity.php | 9 ++++++++- src/Plugin/OgContext/Url.php | 9 ++++++++- 9 files changed, 70 insertions(+), 18 deletions(-) create mode 100644 src/OgContextHandler.php create mode 100644 src/OgContextHandlerInterface.php diff --git a/og.services.yml b/og.services.yml index 64a643f94..3fe0f83d8 100644 --- a/og.services.yml +++ b/og.services.yml @@ -16,6 +16,9 @@ services: og.permission_manager: class: Drupal\og\PermissionManager arguments: ['@event_dispatcher'] + og.context_handler: + class: Drupal\og\OgContextHandler + arguments: ['@config.factory', '@plugin.manager.og.context'] plugin.manager.og.delete_orphans: class: Drupal\og\OgDeleteOrphansPluginManager parent: default_plugin_manager @@ -23,5 +26,5 @@ services: class: Drupal\og\OgFieldsPluginManager parent: default_plugin_manager plugin.manager.og.context: - class: Drupal\og\Plugin\OgContextManager + class: Drupal\og\OgContextManager parent: default_plugin_manager diff --git a/src/Og.php b/src/Og.php index 16b48d835..0fa7f20a2 100644 --- a/src/Og.php +++ b/src/Og.php @@ -590,7 +590,6 @@ public static function removeGroup($entity_type_id, $bundle_id) { return static::groupManager()->removeGroup($entity_type_id, $bundle_id); } - /** * Returns the group manager instance. * @@ -611,18 +610,12 @@ public static function permissionHandler() { } /** - * Get a instance of an OG context plugin. - * - * @param $plugin - * The plugin ID. + * Return the og context handler instance. * - * @return OgContextBase + * @return OgContextHandler */ - static public function ogContextHandler($plugin) { - /** @var OgContextManager $service */ - $service = \Drupal::service('plugin.manager.og.context'); - - return $service->createInstance($plugin); + public static function contextHandler() { + return \Drupal::service('og.context_handler'); } /** diff --git a/src/OgContextBase.php b/src/OgContextBase.php index f99ed88e7..db64f20a7 100644 --- a/src/OgContextBase.php +++ b/src/OgContextBase.php @@ -1,6 +1,6 @@ configFactory = $config_factory; + } + +} \ No newline at end of file diff --git a/src/OgContextHandlerInterface.php b/src/OgContextHandlerInterface.php new file mode 100644 index 000000000..9fce855c2 --- /dev/null +++ b/src/OgContextHandlerInterface.php @@ -0,0 +1,7 @@ +alterInfo('og_context_og_context_info'); $this->setCacheBackend($cache_backend, 'og_context_og_context_plugins'); diff --git a/src/Plugin/OgContext/Entity.php b/src/Plugin/OgContext/Entity.php index 67458ca26..508c6fe88 100644 --- a/src/Plugin/OgContext/Entity.php +++ b/src/Plugin/OgContext/Entity.php @@ -2,7 +2,8 @@ namespace Drupal\og\Plugin\OgContext; -use Drupal\og\Plugin\OgContextBase; +use Drupal\Core\Entity\ContentEntityBase; +use Drupal\og\OgContextBase; /** * @OgContext( @@ -13,4 +14,10 @@ */ class Entity extends OgContextBase { + /** + * {@inheritdoc} + */ + public function getGroup() { + } + } diff --git a/src/Plugin/OgContext/Url.php b/src/Plugin/OgContext/Url.php index 28baa59b4..7bc846538 100644 --- a/src/Plugin/OgContext/Url.php +++ b/src/Plugin/OgContext/Url.php @@ -2,7 +2,8 @@ namespace Drupal\og\Plugin\OgContext; -use Drupal\og\Plugin\OgContextBase; +use Drupal\Core\Entity\ContentEntityBase; +use Drupal\og\OgContextBase; /** * @OgContext( @@ -13,4 +14,10 @@ */ class Url extends OgContextBase { + /** + * {@inheritdoc} + */ + public function getGroup() { + } + } From ed2f4a23777f440151847c347a51bbe6b4489f2a Mon Sep 17 00:00:00 2001 From: Roy Segall Date: Fri, 22 Jul 2016 13:12:27 +0300 Subject: [PATCH 05/32] Iterate over plugins easily. --- src/OgContextHandler.php | 34 +++++++++++++++++++++++++++++-- src/OgContextHandlerInterface.php | 2 +- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/OgContextHandler.php b/src/OgContextHandler.php index 61bdae36f..2e38126b0 100644 --- a/src/OgContextHandler.php +++ b/src/OgContextHandler.php @@ -4,7 +4,7 @@ use Drupal\Core\Config\ConfigFactoryInterface; -class OgContextHandler implements ContextManagerInterface { +class OgContextHandler implements OgContextHandlerInterface { /** * The config factory. @@ -13,14 +13,44 @@ class OgContextHandler implements ContextManagerInterface { */ protected $configFactory; + /** + * The OG context manager. + * + * @var \Drupal\og\OgContextManager + */ + protected $pluginManager; + /** * Constructs an OgManager service. * * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory * The config factory. + * @param \Drupal\og\OgContextManager $context_manager + * The OG context manager. */ - public function __construct(ConfigFactoryInterface $config_factory) { + public function __construct(ConfigFactoryInterface $config_factory, OgContextManager $context_manager) { $this->configFactory = $config_factory; + $this->pluginManager = $context_manager; + } + + /** + * Get a list of an OG context plugins. + * + * @return array + */ + public function getPlugins() { + return $this->pluginManager->getDefinitions(); + } + + /** + * Create an instance of an OG context plugin. + * + * @param $plugin_id + * + * @return OgContextBase + */ + public function getPlugin($plugin_id) { + return $this->pluginManager->createInstance($plugin_id); } } \ No newline at end of file diff --git a/src/OgContextHandlerInterface.php b/src/OgContextHandlerInterface.php index 9fce855c2..cdb9ac8c9 100644 --- a/src/OgContextHandlerInterface.php +++ b/src/OgContextHandlerInterface.php @@ -2,6 +2,6 @@ namespace Drupal\og; -interface ContextManagerInterface { +interface OgContextHandlerInterface { } From f4439c6288ddda55201710249fb0c00e35f5a6ef Mon Sep 17 00:00:00 2001 From: Roy Segall Date: Fri, 22 Jul 2016 14:27:08 +0300 Subject: [PATCH 06/32] Get list of plugins by a logic you desire. --- .../schema/og_context_negotiation.schema.yml | 9 +++- og.module | 29 +++++++++++ src/Entity/OgContextNegotiation.php | 32 ++++++++---- src/OgContextHandler.php | 49 +++++++++++++++---- src/OgContextHandlerInterface.php | 43 ++++++++++++++++ src/Plugin/OgContext/Entity.php | 3 +- src/Plugin/OgContext/Url.php | 3 +- 7 files changed, 144 insertions(+), 24 deletions(-) diff --git a/config/schema/og_context_negotiation.schema.yml b/config/schema/og_context_negotiation.schema.yml index 0e5ddd163..81c256ea3 100644 --- a/config/schema/og_context_negotiation.schema.yml +++ b/config/schema/og_context_negotiation.schema.yml @@ -8,5 +8,12 @@ og.og_context_negotiation.*: label: type: label label: 'Label' - uuid: + description: type: string + label: 'Description' + status: + type: boolean + label: 'Status' + weight: + type: integer + label: 'Weight' diff --git a/og.module b/og.module index e38d8ea0d..010f4952b 100755 --- a/og.module +++ b/og.module @@ -13,6 +13,7 @@ use Drupal\Core\Session\AccountInterface; use Drupal\og\Entity\OgMembership; use Drupal\og\Og; use Drupal\og\OgAccess; +use Drupal\og\OgContextHandlerInterface; use Drupal\og\OgGroupAudienceHelper; use Drupal\og\OgMembershipInterface; use Drupal\og\OgRoleInterface; @@ -197,6 +198,34 @@ 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() { + $plugins = Og::contextHandler()->getPlugins(['return_mode' => OgContextHandlerInterface::RETURN_ALL]); + $og_context_storage = \Drupal::entityTypeManager()->getStorage('og_context_negotiation'); + $og_context_config = $og_context_storage->loadMultiple(); + + $weight = 0; + foreach ($plugins as $plugin) { + if (in_array($plugin['id'], array_keys($og_context_config))) { + // The negotiation plugin already registered. + continue; + } + + // Registering a new negotiation plugin.s + $og_context_storage->create([ + 'id' => $plugin['id'], + 'label' => $plugin['label'], + 'description' => $plugin['description'], + 'status' => FALSE, + 'weight' => $weight, + ])->save(); + + $weight++; + } +} + /** * Implements hook_help(). */ diff --git a/src/Entity/OgContextNegotiation.php b/src/Entity/OgContextNegotiation.php index 3fe0f9e44..b28753567 100644 --- a/src/Entity/OgContextNegotiation.php +++ b/src/Entity/OgContextNegotiation.php @@ -17,12 +17,7 @@ * id = "og_context_negotiation", * label = @Translation("OG context negotiation"), * handlers = { - * "list_builder" = "Drupal\og\OgContextNegotiationListBuilder", - * "form" = { - * "add" = "Drupal\og\Form\OgContextNegotiationForm", - * "edit" = "Drupal\og\Form\OgContextNegotiationForm", - * "delete" = "Drupal\og\Form\OgContextNegotiationDeleteForm" - * } + * "list_builder" = "Drupal\og\OgContextNegotiationListBuilder" * }, * config_prefix = "og_context_negotiation", * admin_permission = "administer site configuration", @@ -32,14 +27,12 @@ * "uuid" = "uuid" * }, * links = { - * "canonical" = "/admin/structure/og_context_negotiation/{og_context_negotiation}", - * "edit-form" = "/admin/structure/og_context_negotiation/{og_context_negotiation}/edit", - * "delete-form" = "/admin/structure/og_context_negotiation/{og_context_negotiation}/delete", * "collection" = "/admin/structure/visibility_group" * } * ) */ class OgContextNegotiation extends ConfigEntityBase implements OgContextNegotiationInterface { + /** * The OG context negotiation ID. * @@ -54,4 +47,25 @@ class OgContextNegotiation extends ConfigEntityBase implements OgContextNegotiat */ 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; + } diff --git a/src/OgContextHandler.php b/src/OgContextHandler.php index 2e38126b0..7e8a37aef 100644 --- a/src/OgContextHandler.php +++ b/src/OgContextHandler.php @@ -3,6 +3,7 @@ namespace Drupal\og; use Drupal\Core\Config\ConfigFactoryInterface; +use Drupal\og\Entity\OgContextNegotiation; class OgContextHandler implements OgContextHandlerInterface { @@ -34,23 +35,51 @@ public function __construct(ConfigFactoryInterface $config_factory, OgContextMan } /** - * Get a list of an OG context plugins. - * - * @return array + * {@inheritdoc} */ - public function getPlugins() { - return $this->pluginManager->getDefinitions(); + public function getPlugins($config = []) { + $config += [ + // sort the plugins by the weight defined in negotiation schema. + 'sort_by_weight' => TRUE, + 'return_mode' => OgContextHandlerInterface::RETURN_ONLY_ACTIVE, + ]; + + $plugins = $this->pluginManager->getDefinitions(); + + if ($config['return_mode'] != OgContextHandlerInterface::RETURN_ALL) { + + /** @var OgContextNegotiation[] $og_context_config */ + $og_context_config = \Drupal::entityTypeManager()->getStorage('og_context_negotiation')->loadMultiple(); + + foreach ($og_context_config as $context) { + if ($config['mode'] == OgContextHandlerInterface::RETURN_ONLY_ACTIVE) { + $condition = $context->get('status') === FALSE; + } + else { + $condition = !in_array($context->id(), array_keys($plugins)); + } + + if ($condition) { + unset($plugins[$context->id()]); + } + } + } + + return $plugins; } /** - * Create an instance of an OG context plugin. - * - * @param $plugin_id - * - * @return OgContextBase + * {@inheritdoc} */ public function getPlugin($plugin_id) { return $this->pluginManager->createInstance($plugin_id); } + /** + * {@inheritdoc} + */ + public function updatePlugin($config = []) { + + } + } \ No newline at end of file diff --git a/src/OgContextHandlerInterface.php b/src/OgContextHandlerInterface.php index cdb9ac8c9..61fce6eb4 100644 --- a/src/OgContextHandlerInterface.php +++ b/src/OgContextHandlerInterface.php @@ -4,4 +4,47 @@ interface OgContextHandlerInterface { + /** + * Return only plugins which active in the negotiation schema. + */ + const RETURN_ONLY_ACTIVE = 1; + + /** + * Return only plugins which stored in he negotiation schema with a status of + * enabled/disabled. + */ + const RETURN_ONLY_IN_STORAGE = 2; + + /** + * Return all the plugins without considering the negotiation schema. + */ + const RETURN_ALL = 3; + + /** + * Get a list of an OG context plugins. + * + * @param array $config + * @return array + */ + public function getPlugins($config = []); + + /** + * Create an instance of an OG context plugin. + * + * @param $plugin_id + * + * @return OgContextBase + */ + public function getPlugin($plugin_id); + + /** + * Update plugin settings. + * + * @param array $config + * The plugins settings. + * + * @return boolean + */ + public function updatePlugin($config = []); + } diff --git a/src/Plugin/OgContext/Entity.php b/src/Plugin/OgContext/Entity.php index 508c6fe88..b27aad4f3 100644 --- a/src/Plugin/OgContext/Entity.php +++ b/src/Plugin/OgContext/Entity.php @@ -2,13 +2,12 @@ namespace Drupal\og\Plugin\OgContext; -use Drupal\Core\Entity\ContentEntityBase; use Drupal\og\OgContextBase; /** * @OgContext( * id = "entity", - * title = "Entity", + * label = "Entity", * description = @Translation("Get the group from the current entity, by checking if it is a group or a group content entity.") * ) */ diff --git a/src/Plugin/OgContext/Url.php b/src/Plugin/OgContext/Url.php index 7bc846538..5c1fc4bd9 100644 --- a/src/Plugin/OgContext/Url.php +++ b/src/Plugin/OgContext/Url.php @@ -2,13 +2,12 @@ namespace Drupal\og\Plugin\OgContext; -use Drupal\Core\Entity\ContentEntityBase; use Drupal\og\OgContextBase; /** * @OgContext( * id = "url", - * type = "URL", + * label = "URL", * description = @Translation("Get the group from the given URL.") * ) */ From 2ca93c85603b7e3fcfd27ba60dcdd9998e2e0c24 Mon Sep 17 00:00:00 2001 From: Roy Segall Date: Fri, 22 Jul 2016 14:47:05 +0300 Subject: [PATCH 07/32] Minor fixes. --- og.install | 7 +++++++ og.module | 23 +---------------------- src/OgContextHandler.php | 29 ++++++++++++++++++++++++++++- 3 files changed, 36 insertions(+), 23 deletions(-) diff --git a/og.install b/og.install index 0365819a8..6681d79c2 100644 --- a/og.install +++ b/og.install @@ -75,3 +75,10 @@ function og_schema() { return $schema; } +/** + * Implements hook_install(). + */ +function og_install() { + \Drupal\og\Og::contextHandler()->updateConfigStorage(); +} + diff --git a/og.module b/og.module index 010f4952b..73d931a81 100755 --- a/og.module +++ b/og.module @@ -202,28 +202,7 @@ function og_entity_create_access(AccountInterface $account, array $context, $bun * Implements hook_cache_flush(). */ function og_cache_flush() { - $plugins = Og::contextHandler()->getPlugins(['return_mode' => OgContextHandlerInterface::RETURN_ALL]); - $og_context_storage = \Drupal::entityTypeManager()->getStorage('og_context_negotiation'); - $og_context_config = $og_context_storage->loadMultiple(); - - $weight = 0; - foreach ($plugins as $plugin) { - if (in_array($plugin['id'], array_keys($og_context_config))) { - // The negotiation plugin already registered. - continue; - } - - // Registering a new negotiation plugin.s - $og_context_storage->create([ - 'id' => $plugin['id'], - 'label' => $plugin['label'], - 'description' => $plugin['description'], - 'status' => FALSE, - 'weight' => $weight, - ])->save(); - - $weight++; - } + Og::contextHandler()->updateConfigStorage(); } /** diff --git a/src/OgContextHandler.php b/src/OgContextHandler.php index 7e8a37aef..4d084bec9 100644 --- a/src/OgContextHandler.php +++ b/src/OgContextHandler.php @@ -52,7 +52,7 @@ public function getPlugins($config = []) { $og_context_config = \Drupal::entityTypeManager()->getStorage('og_context_negotiation')->loadMultiple(); foreach ($og_context_config as $context) { - if ($config['mode'] == OgContextHandlerInterface::RETURN_ONLY_ACTIVE) { + if ($config['return_mode'] == OgContextHandlerInterface::RETURN_ONLY_ACTIVE) { $condition = $context->get('status') === FALSE; } else { @@ -82,4 +82,31 @@ public function updatePlugin($config = []) { } + public function updateConfigStorage() { + $plugins = $this->getPlugins(['return_mode' => OgContextHandlerInterface::RETURN_ALL]); + + // todo user storage injection. + $og_context_storage = \Drupal::entityTypeManager()->getStorage('og_context_negotiation'); + $og_context_config = $og_context_storage->loadMultiple(); + + $weight = 0; + foreach ($plugins as $plugin) { + if (in_array($plugin['id'], array_keys($og_context_config))) { + // The negotiation plugin already registered. + continue; + } + + // Registering a new negotiation plugin.s + $og_context_storage->create([ + 'id' => $plugin['id'], + 'label' => $plugin['label'], + 'description' => $plugin['description'], + 'status' => FALSE, + 'weight' => $weight, + ])->save(); + + $weight++; + } + } + } \ No newline at end of file From 273dd204711fd59112e166383ae8167dc04ed0eb Mon Sep 17 00:00:00 2001 From: Roy Segall Date: Fri, 22 Jul 2016 18:49:55 +0300 Subject: [PATCH 08/32] Get the plugins in the right order. --- og.services.yml | 2 +- src/OgContextHandler.php | 39 ++++++++++++++++++++++++++----- src/OgContextHandlerInterface.php | 12 ++++++++-- 3 files changed, 44 insertions(+), 9 deletions(-) diff --git a/og.services.yml b/og.services.yml index 3fe0f83d8..2bb3101c2 100644 --- a/og.services.yml +++ b/og.services.yml @@ -18,7 +18,7 @@ services: arguments: ['@event_dispatcher'] og.context_handler: class: Drupal\og\OgContextHandler - arguments: ['@config.factory', '@plugin.manager.og.context'] + arguments: ['@config.factory', '@plugin.manager.og.context', '@entity_type.manager'] plugin.manager.og.delete_orphans: class: Drupal\og\OgDeleteOrphansPluginManager parent: default_plugin_manager diff --git a/src/OgContextHandler.php b/src/OgContextHandler.php index 4d084bec9..63fc3cb89 100644 --- a/src/OgContextHandler.php +++ b/src/OgContextHandler.php @@ -3,6 +3,7 @@ namespace Drupal\og; use Drupal\Core\Config\ConfigFactoryInterface; +use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\og\Entity\OgContextNegotiation; class OgContextHandler implements OgContextHandlerInterface { @@ -21,6 +22,13 @@ class OgContextHandler implements OgContextHandlerInterface { */ protected $pluginManager; + /** + * The entity manager. + * + * @var \Drupal\Core\Entity\EntityStorageInterface + */ + protected $storage; + /** * Constructs an OgManager service. * @@ -28,10 +36,13 @@ class OgContextHandler implements OgContextHandlerInterface { * The config factory. * @param \Drupal\og\OgContextManager $context_manager * The OG context manager. + * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_manager + * The entity type manager. */ - public function __construct(ConfigFactoryInterface $config_factory, OgContextManager $context_manager) { + public function __construct(ConfigFactoryInterface $config_factory, OgContextManager $context_manager, EntityTypeManagerInterface $entity_manager) { $this->configFactory = $config_factory; $this->pluginManager = $context_manager; + $this->storage = $entity_manager->getStorage('og_context_negotiation'); } /** @@ -39,7 +50,6 @@ public function __construct(ConfigFactoryInterface $config_factory, OgContextMan */ public function getPlugins($config = []) { $config += [ - // sort the plugins by the weight defined in negotiation schema. 'sort_by_weight' => TRUE, 'return_mode' => OgContextHandlerInterface::RETURN_ONLY_ACTIVE, ]; @@ -49,7 +59,7 @@ public function getPlugins($config = []) { if ($config['return_mode'] != OgContextHandlerInterface::RETURN_ALL) { /** @var OgContextNegotiation[] $og_context_config */ - $og_context_config = \Drupal::entityTypeManager()->getStorage('og_context_negotiation')->loadMultiple(); + $og_context_config = $this->storage->loadMultiple(); foreach ($og_context_config as $context) { if ($config['return_mode'] == OgContextHandlerInterface::RETURN_ONLY_ACTIVE) { @@ -65,6 +75,14 @@ public function getPlugins($config = []) { } } + if ($config['sort_by_weight']) { + $og_context_config = $this->storage->loadMultiple(); + + uasort($plugins, function($a, $b) use($og_context_config) { + return $og_context_config[$a['id']]->get('weight') > $og_context_config[$b['id']]->get('weight') ? 1 : -1; + }); + } + return $plugins; } @@ -78,15 +96,24 @@ public function getPlugin($plugin_id) { /** * {@inheritdoc} */ - public function updatePlugin($config = []) { + public function updatePlugin($plugin_id, $config = []) { + /** @var OgContextNegotiation $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(['return_mode' => OgContextHandlerInterface::RETURN_ALL]); - // todo user storage injection. - $og_context_storage = \Drupal::entityTypeManager()->getStorage('og_context_negotiation'); + $og_context_storage = $this->storage; $og_context_config = $og_context_storage->loadMultiple(); $weight = 0; diff --git a/src/OgContextHandlerInterface.php b/src/OgContextHandlerInterface.php index 61fce6eb4..79889a2b3 100644 --- a/src/OgContextHandlerInterface.php +++ b/src/OgContextHandlerInterface.php @@ -40,11 +40,19 @@ public function getPlugin($plugin_id); /** * Update plugin settings. * + * @param $plugin_id + * The plugin ID. * @param array $config * The plugins settings. + * @return bool + */ + public function updatePlugin($plugin_id, $config = []); + + /** + * Iterate over the plugins and register a new plugin. * - * @return boolean + * Will be invoked when installing OG and clearing the cache. */ - public function updatePlugin($config = []); + public function updateConfigStorage(); } From 9f5d25154b16c7d6b5e6b064e2fae474eec67549 Mon Sep 17 00:00:00 2001 From: Roy Segall Date: Fri, 22 Jul 2016 20:18:40 +0300 Subject: [PATCH 09/32] Get the group from the url. --- src/OgContextBase.php | 27 +++++++++++++- src/OgContextHandler.php | 15 ++++++++ src/OgContextHandlerInterface.php | 9 +++++ src/Plugin/OgContext/Entity.php | 1 + src/Plugin/OgContext/Url.php | 60 +++++++++++++++++++++++++++++++ 5 files changed, 111 insertions(+), 1 deletion(-) diff --git a/src/OgContextBase.php b/src/OgContextBase.php index db64f20a7..688a8c46f 100644 --- a/src/OgContextBase.php +++ b/src/OgContextBase.php @@ -3,10 +3,35 @@ namespace Drupal\og; use Drupal\Component\Plugin\PluginBase; +use Drupal\Core\Plugin\ContainerFactoryPluginInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * Base class for OG context plugins. */ -abstract class OgContextBase extends PluginBase implements OgContextInterface { +abstract class OgContextBase extends PluginBase implements OgContextInterface, ContainerFactoryPluginInterface { + + /** + * Creates an instance of the plugin. + * + * @param \Symfony\Component\DependencyInjection\ContainerInterface $container + * The container to pull out services used in the plugin. + * @param array $configuration + * A configuration array containing information about the plugin instance. + * @param string $plugin_id + * The plugin ID for the plugin instance. + * @param mixed $plugin_definition + * The plugin implementation definition. + * + * @return static + * Returns an instance of this plugin. + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition + ); + } } diff --git a/src/OgContextHandler.php b/src/OgContextHandler.php index 63fc3cb89..0a1221f15 100644 --- a/src/OgContextHandler.php +++ b/src/OgContextHandler.php @@ -45,6 +45,21 @@ public function __construct(ConfigFactoryInterface $config_factory, OgContextMan $this->storage = $entity_manager->getStorage('og_context_negotiation'); } + /** + * {@inheritdoc} + */ + public function getGroup() { + $plugins = $this->getPlugins(); + + foreach ($plugins as $plugin) { + if ($group = $this->getPlugin($plugin['id'])->getGroup()) { + return $group; + } + } + + return NULL; + } + /** * {@inheritdoc} */ diff --git a/src/OgContextHandlerInterface.php b/src/OgContextHandlerInterface.php index 79889a2b3..eaf22bf4a 100644 --- a/src/OgContextHandlerInterface.php +++ b/src/OgContextHandlerInterface.php @@ -2,6 +2,8 @@ namespace Drupal\og; +use Drupal\Core\Entity\ContentEntityBase; + interface OgContextHandlerInterface { /** @@ -20,6 +22,13 @@ interface OgContextHandlerInterface { */ const RETURN_ALL = 3; + /** + * Get the current viewed group. + * + * @return NULL|ContentEntityBase + */ + public function getGroup(); + /** * Get a list of an OG context plugins. * diff --git a/src/Plugin/OgContext/Entity.php b/src/Plugin/OgContext/Entity.php index b27aad4f3..e6603b198 100644 --- a/src/Plugin/OgContext/Entity.php +++ b/src/Plugin/OgContext/Entity.php @@ -3,6 +3,7 @@ namespace Drupal\og\Plugin\OgContext; use Drupal\og\OgContextBase; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * @OgContext( diff --git a/src/Plugin/OgContext/Url.php b/src/Plugin/OgContext/Url.php index 5c1fc4bd9..0a4788d0e 100644 --- a/src/Plugin/OgContext/Url.php +++ b/src/Plugin/OgContext/Url.php @@ -2,7 +2,12 @@ namespace Drupal\og\Plugin\OgContext; +use Drupal\Core\Entity\ContentEntityBase; +use Drupal\Core\Routing\RouteMatchInterface; +use Drupal\og\Og; use Drupal\og\OgContextBase; +use Drupal\og\GroupManager; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * @OgContext( @@ -13,10 +18,65 @@ */ class Url extends OgContextBase { + /** + * The route match service. + * + * @var RouteMatchInterface + */ + protected $routeMatch; + + /** + * The group manager service. + * + * @var \Drupal\og\GroupManager + */ + protected $groupManger; + + /** + * Constructs a Drupal\Component\Plugin\PluginBase object. + * + * @param array $configuration + * A configuration array containing information about the plugin instance. + * @param string $plugin_id + * The plugin_id for the plugin instance. + * @param mixed $plugin_definition + * The plugin implementation definition. + * @param \Drupal\Core\Routing\RouteMatchInterface $route_match + * The route match service. + * @param \Drupal\og\GroupManager $group_manager + */ + public function __construct(array $configuration, $plugin_id, $plugin_definition, RouteMatchInterface $route_match, GroupManager $group_manager) { + parent::__construct($configuration, $plugin_id, $plugin_definition); + $this->routeMatch = $route_match; + $this->groupManger = $group_manager; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('current_route_match'), + $container->get('og.group.manager') + ); + } + /** * {@inheritdoc} */ public function getGroup() { + foreach ($this->routeMatch->getParameters() as $parameter) { + if (!($parameter instanceof ContentEntityBase)) { + continue; + } + + if ($this->groupManger->isGroup($parameter->getEntityTypeId(), $parameter->bundle())) { + return $parameter; + } + } } } From 55e06bb95036ac4063627f3ea586d166216bc4d0 Mon Sep 17 00:00:00 2001 From: Roy Segall Date: Fri, 22 Jul 2016 20:41:41 +0300 Subject: [PATCH 10/32] Pushing work. --- src/Plugin/OgContext/Entity.php | 59 ++++++++++++++++++++++++++++++++- src/Plugin/OgContext/Url.php | 59 --------------------------------- 2 files changed, 58 insertions(+), 60 deletions(-) diff --git a/src/Plugin/OgContext/Entity.php b/src/Plugin/OgContext/Entity.php index e6603b198..272b266c8 100644 --- a/src/Plugin/OgContext/Entity.php +++ b/src/Plugin/OgContext/Entity.php @@ -2,6 +2,9 @@ namespace Drupal\og\Plugin\OgContext; +use Drupal\Core\Entity\ContentEntityBase; +use Drupal\Core\Routing\RouteMatchInterface; +use Drupal\og\GroupManager; use Drupal\og\OgContextBase; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -14,10 +17,64 @@ */ class Entity extends OgContextBase { + /** + * The route match service. + * + * @var RouteMatchInterface + */ + protected $routeMatch; + + /** + * The group manager service. + * + * @var \Drupal\og\GroupManager + */ + protected $groupManger; + + /** + * Constructs a Drupal\Component\Plugin\PluginBase object. + * + * @param array $configuration + * A configuration array containing information about the plugin instance. + * @param string $plugin_id + * The plugin_id for the plugin instance. + * @param mixed $plugin_definition + * The plugin implementation definition. + * @param \Drupal\Core\Routing\RouteMatchInterface $route_match + * The route match service. + * @param \Drupal\og\GroupManager $group_manager + */ + public function __construct(array $configuration, $plugin_id, $plugin_definition, RouteMatchInterface $route_match, GroupManager $group_manager) { + parent::__construct($configuration, $plugin_id, $plugin_definition); + $this->routeMatch = $route_match; + $this->groupManger = $group_manager; + } + /** * {@inheritdoc} */ - public function getGroup() { + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('current_route_match'), + $container->get('og.group.manager') + ); } + /** + * {@inheritdoc} + */ + public function getGroup() { + foreach ($this->routeMatch->getParameters() as $parameter) { + if (!($parameter instanceof ContentEntityBase)) { + continue; + } + + if ($this->groupManger->isGroup($parameter->getEntityTypeId(), $parameter->bundle())) { + return $parameter; + } + } + } } diff --git a/src/Plugin/OgContext/Url.php b/src/Plugin/OgContext/Url.php index 0a4788d0e..e7d884714 100644 --- a/src/Plugin/OgContext/Url.php +++ b/src/Plugin/OgContext/Url.php @@ -3,11 +3,7 @@ namespace Drupal\og\Plugin\OgContext; use Drupal\Core\Entity\ContentEntityBase; -use Drupal\Core\Routing\RouteMatchInterface; -use Drupal\og\Og; use Drupal\og\OgContextBase; -use Drupal\og\GroupManager; -use Symfony\Component\DependencyInjection\ContainerInterface; /** * @OgContext( @@ -18,65 +14,10 @@ */ class Url extends OgContextBase { - /** - * The route match service. - * - * @var RouteMatchInterface - */ - protected $routeMatch; - - /** - * The group manager service. - * - * @var \Drupal\og\GroupManager - */ - protected $groupManger; - - /** - * Constructs a Drupal\Component\Plugin\PluginBase object. - * - * @param array $configuration - * A configuration array containing information about the plugin instance. - * @param string $plugin_id - * The plugin_id for the plugin instance. - * @param mixed $plugin_definition - * The plugin implementation definition. - * @param \Drupal\Core\Routing\RouteMatchInterface $route_match - * The route match service. - * @param \Drupal\og\GroupManager $group_manager - */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, RouteMatchInterface $route_match, GroupManager $group_manager) { - parent::__construct($configuration, $plugin_id, $plugin_definition); - $this->routeMatch = $route_match; - $this->groupManger = $group_manager; - } - - /** - * {@inheritdoc} - */ - public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { - return new static( - $configuration, - $plugin_id, - $plugin_definition, - $container->get('current_route_match'), - $container->get('og.group.manager') - ); - } - /** * {@inheritdoc} */ public function getGroup() { - foreach ($this->routeMatch->getParameters() as $parameter) { - if (!($parameter instanceof ContentEntityBase)) { - continue; - } - - if ($this->groupManger->isGroup($parameter->getEntityTypeId(), $parameter->bundle())) { - return $parameter; - } - } } } From bc82a4503fda5527feafd2496ddddab8537148b8 Mon Sep 17 00:00:00 2001 From: Roy Segall Date: Fri, 22 Jul 2016 23:10:34 +0300 Subject: [PATCH 11/32] Get the group from a group content. --- src/Plugin/OgContext/Entity.php | 9 +++++++++ src/Plugin/OgContext/Url.php | 23 ----------------------- 2 files changed, 9 insertions(+), 23 deletions(-) delete mode 100644 src/Plugin/OgContext/Url.php diff --git a/src/Plugin/OgContext/Entity.php b/src/Plugin/OgContext/Entity.php index 272b266c8..89bdf4347 100644 --- a/src/Plugin/OgContext/Entity.php +++ b/src/Plugin/OgContext/Entity.php @@ -5,6 +5,7 @@ use Drupal\Core\Entity\ContentEntityBase; use Drupal\Core\Routing\RouteMatchInterface; use Drupal\og\GroupManager; +use Drupal\og\Og; use Drupal\og\OgContextBase; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -75,6 +76,14 @@ public function getGroup() { if ($this->groupManger->isGroup($parameter->getEntityTypeId(), $parameter->bundle())) { return $parameter; } + + if (!Og::isGroupContent($parameter->getEntityTypeId(), $parameter->bundle())) { + continue; + } + + foreach (Og::getGroups($parameter) as $groups) { + return reset($groups); + } } } } diff --git a/src/Plugin/OgContext/Url.php b/src/Plugin/OgContext/Url.php deleted file mode 100644 index e7d884714..000000000 --- a/src/Plugin/OgContext/Url.php +++ /dev/null @@ -1,23 +0,0 @@ - Date: Fri, 22 Jul 2016 23:22:17 +0300 Subject: [PATCH 12/32] Get all the groups. --- src/Plugin/OgContext/Entity.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Plugin/OgContext/Entity.php b/src/Plugin/OgContext/Entity.php index 89bdf4347..819627140 100644 --- a/src/Plugin/OgContext/Entity.php +++ b/src/Plugin/OgContext/Entity.php @@ -81,9 +81,7 @@ public function getGroup() { continue; } - foreach (Og::getGroups($parameter) as $groups) { - return reset($groups); - } + return Og::getGroups($parameter); } } } From 27c3fac5b088cbe2921dabe5524e6117ca3c4c6d Mon Sep 17 00:00:00 2001 From: Roy Segall Date: Sat, 23 Jul 2016 11:47:44 +0300 Subject: [PATCH 13/32] Remove a line in the end of the file. --- og.install | 1 - 1 file changed, 1 deletion(-) diff --git a/og.install b/og.install index 6681d79c2..ba5b0a0a2 100644 --- a/og.install +++ b/og.install @@ -81,4 +81,3 @@ function og_schema() { function og_install() { \Drupal\og\Og::contextHandler()->updateConfigStorage(); } - From b263d46bba80a1c9a8071e591d45ad4fd2d01d39 Mon Sep 17 00:00:00 2001 From: Roy Segall Date: Sat, 23 Jul 2016 12:01:23 +0300 Subject: [PATCH 14/32] My own CR fixes. --- src/Entity/OgContextNegotiation.php | 6 ----- src/OgContextHandler.php | 2 +- src/OgContextHandlerInterface.php | 19 +++++++++++++ src/OgContextNegotiationListBuilder.php | 36 ------------------------- 4 files changed, 20 insertions(+), 43 deletions(-) delete mode 100644 src/OgContextNegotiationListBuilder.php diff --git a/src/Entity/OgContextNegotiation.php b/src/Entity/OgContextNegotiation.php index b28753567..ed77b295d 100644 --- a/src/Entity/OgContextNegotiation.php +++ b/src/Entity/OgContextNegotiation.php @@ -16,18 +16,12 @@ * @ConfigEntityType( * id = "og_context_negotiation", * label = @Translation("OG context negotiation"), - * handlers = { - * "list_builder" = "Drupal\og\OgContextNegotiationListBuilder" - * }, * config_prefix = "og_context_negotiation", * admin_permission = "administer site configuration", * entity_keys = { * "id" = "id", * "label" = "label", * "uuid" = "uuid" - * }, - * links = { - * "collection" = "/admin/structure/visibility_group" * } * ) */ diff --git a/src/OgContextHandler.php b/src/OgContextHandler.php index 0a1221f15..21f2f601e 100644 --- a/src/OgContextHandler.php +++ b/src/OgContextHandler.php @@ -151,4 +151,4 @@ public function updateConfigStorage() { } } -} \ No newline at end of file +} diff --git a/src/OgContextHandlerInterface.php b/src/OgContextHandlerInterface.php index eaf22bf4a..af4d0b021 100644 --- a/src/OgContextHandlerInterface.php +++ b/src/OgContextHandlerInterface.php @@ -33,6 +33,23 @@ public function getGroup(); * Get a list of an OG context plugins. * * @param array $config + * Array of settings: + * sort_by_weight - Determine if the plugins will be order by the weight of + * corresponding OG context negotiation entity entry. + * + * return_mode - Determine which plugins will be returned: + * OgContextHandlerInterface::RETURN_ALL - will return all the plugins. + * Including plugins which don't have a corresponding OG context + * negotiation entity entry. + * + * OgContextHandlerInterface::RETURN_ONLY_ACTIVE - will return only + * plugins which their corresponding OG context negotiation entity entry + * status is set to true. + * + * OgContextHandlerInterface::RETURN_ONLY_IN_STORAGE - will return only + * plugins with a corresponding OG context negotiation entity entry no + * matter what is the status. + * * @return array */ public function getPlugins($config = []); @@ -41,6 +58,7 @@ public function getPlugins($config = []); * Create an instance of an OG context plugin. * * @param $plugin_id + * The plugin ID. * * @return OgContextBase */ @@ -53,6 +71,7 @@ public function getPlugin($plugin_id); * The plugin ID. * @param array $config * The plugins settings. + * * @return bool */ public function updatePlugin($plugin_id, $config = []); diff --git a/src/OgContextNegotiationListBuilder.php b/src/OgContextNegotiationListBuilder.php deleted file mode 100644 index e03525fd2..000000000 --- a/src/OgContextNegotiationListBuilder.php +++ /dev/null @@ -1,36 +0,0 @@ -t('OG context negotiation'); - $header['id'] = $this->t('Machine name'); - return $header + parent::buildHeader(); - } - - /** - * {@inheritdoc} - */ - public function buildRow(EntityInterface $entity) { - $row['label'] = $this->getLabel($entity); - $row['id'] = $entity->id(); - // You probably want a few more properties here... - return $row + parent::buildRow($entity); - } - -} From a2f5eb7a1f986768c5e6432d9ec9f46933e7b39a Mon Sep 17 00:00:00 2001 From: Roy Segall Date: Sun, 24 Jul 2016 23:12:01 +0300 Subject: [PATCH 15/32] Adding some tests. --- src/OgContextHandler.php | 22 ++--- src/OgContextHandlerInterface.php | 16 ++-- src/Plugin/OgContext/CurrentUser.php | 59 ++++++++++++ tests/src/Kernel/Entity/OgContextTest.php | 108 ++++++++++++++++++++++ 4 files changed, 181 insertions(+), 24 deletions(-) create mode 100644 src/Plugin/OgContext/CurrentUser.php create mode 100644 tests/src/Kernel/Entity/OgContextTest.php diff --git a/src/OgContextHandler.php b/src/OgContextHandler.php index 21f2f601e..fc7c66034 100644 --- a/src/OgContextHandler.php +++ b/src/OgContextHandler.php @@ -63,22 +63,18 @@ public function getGroup() { /** * {@inheritdoc} */ - public function getPlugins($config = []) { - $config += [ - 'sort_by_weight' => TRUE, - 'return_mode' => OgContextHandlerInterface::RETURN_ONLY_ACTIVE, - ]; + public function getPlugins($return_mode = OgContextHandlerInterface::RETURN_ONLY_ACTIVE) { - $plugins = $this->pluginManager->getDefinitions(); + /** @var OgContextNegotiation[] $og_context_config */ + $og_context_config = $this->storage->loadMultiple(); - if ($config['return_mode'] != OgContextHandlerInterface::RETURN_ALL) { + $plugins = $this->pluginManager->getDefinitions(); - /** @var OgContextNegotiation[] $og_context_config */ - $og_context_config = $this->storage->loadMultiple(); + if ($return_mode != OgContextHandlerInterface::RETURN_ALL) { foreach ($og_context_config as $context) { - if ($config['return_mode'] == OgContextHandlerInterface::RETURN_ONLY_ACTIVE) { - $condition = $context->get('status') === FALSE; + if ($return_mode == OgContextHandlerInterface::RETURN_ONLY_ACTIVE) { + $condition = $context->get('status') == FALSE; } else { $condition = !in_array($context->id(), array_keys($plugins)); @@ -90,9 +86,7 @@ public function getPlugins($config = []) { } } - if ($config['sort_by_weight']) { - $og_context_config = $this->storage->loadMultiple(); - + if (!empty($og_context_config)) { uasort($plugins, function($a, $b) use($og_context_config) { return $og_context_config[$a['id']]->get('weight') > $og_context_config[$b['id']]->get('weight') ? 1 : -1; }); diff --git a/src/OgContextHandlerInterface.php b/src/OgContextHandlerInterface.php index af4d0b021..725aa237d 100644 --- a/src/OgContextHandlerInterface.php +++ b/src/OgContextHandlerInterface.php @@ -32,27 +32,23 @@ public function getGroup(); /** * Get a list of an OG context plugins. * - * @param array $config - * Array of settings: - * sort_by_weight - Determine if the plugins will be order by the weight of - * corresponding OG context negotiation entity entry. + * @param int $return_mode + * Determine which plugins will be returned: * - * return_mode - Determine which plugins will be returned: - * OgContextHandlerInterface::RETURN_ALL - will return all the plugins. + * OgContextHandlerInterface::RETURN_ALL - will return all the plugins. * Including plugins which don't have a corresponding OG context * negotiation entity entry. * - * OgContextHandlerInterface::RETURN_ONLY_ACTIVE - will return only + * OgContextHandlerInterface::RETURN_ONLY_ACTIVE - will return only * plugins which their corresponding OG context negotiation entity entry * status is set to true. * - * OgContextHandlerInterface::RETURN_ONLY_IN_STORAGE - will return only + * OgContextHandlerInterface::RETURN_ONLY_IN_STORAGE - will return only * plugins with a corresponding OG context negotiation entity entry no * matter what is the status. - * * @return array */ - public function getPlugins($config = []); + public function getPlugins($return_mode); /** * Create an instance of an OG context plugin. diff --git a/src/Plugin/OgContext/CurrentUser.php b/src/Plugin/OgContext/CurrentUser.php new file mode 100644 index 000000000..446be9b50 --- /dev/null +++ b/src/Plugin/OgContext/CurrentUser.php @@ -0,0 +1,59 @@ +currentUser = $current_user; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('current_user') + ); + } + + /** + * {@inheritdoc} + */ + public function getGroup() { + } + +} diff --git a/tests/src/Kernel/Entity/OgContextTest.php b/tests/src/Kernel/Entity/OgContextTest.php new file mode 100644 index 000000000..06626d6c6 --- /dev/null +++ b/tests/src/Kernel/Entity/OgContextTest.php @@ -0,0 +1,108 @@ +installConfig(['og']); + + Og::contextHandler()->updateConfigStorage(); + } + + /** + * Test that OgContext handler will return the list according to the passed + * argument. + */ + function testOgContextPluginsList() { + $plugins = Og::contextHandler()->getPlugins(OgContextHandlerInterface::RETURN_ALL); + + // Check we got the plugins we know that exists. + if (empty($plugins['current_user']) || empty($plugins['entity'])) { + $this->fail('The expected plugins, Current user and Entity, was not found in the plugins list'); + } + + // Get all the plugins in the storage which have a matching storage. + $plugins = Og::contextHandler()->getPlugins(OgContextHandlerInterface::RETURN_ONLY_IN_STORAGE); + + if (empty($plugins['current_user']) || empty($plugins['entity'])) { + $this->fail('The expected plugins, Current user and Entity, was not found in the plugins list'); + } + + // Get all active plugins. + $plugins = Og::contextHandler()->getPlugins(OgContextHandlerInterface::RETURN_ONLY_ACTIVE); + + $this->assertEmpty($plugins); + + // Enable a plugin. + Og::contextHandler()->updatePlugin('entity', ['status' => 1]); + + $plugins = Og::contextHandler()->getPlugins(OgContextHandlerInterface::RETURN_ONLY_ACTIVE); + $this->assertArrayHasKey('entity', $plugins); + $this->assertArrayNotHasKey('current_user', $plugins); + + // Enable the second plugin and change the weight. + Og::contextHandler()->updatePlugin('entity', ['weight' => 1]); + Og::contextHandler()->updatePlugin('current_user', ['weight' => 0, 'status' => 1]); + + $plugins = Og::contextHandler()->getPlugins(OgContextHandlerInterface::RETURN_ONLY_ACTIVE); + $this->assertArrayHasKey('entity', $plugins); + $this->assertArrayHasKey('current_user', $plugins); + + $get_plugins_location = function($plugins) { + return[ + array_search('entity', $plugins), + array_search('current_user', $plugins), + ]; + }; + + list($entity_position, $current_user_position) = $get_plugins_location(array_keys($plugins)); + + $this->assertTrue($entity_position > $current_user_position); + + // Change the weight. + Og::contextHandler()->updatePlugin('entity', ['weight' => -1]); + + $plugins = Og::contextHandler()->getPlugins(OgContextHandlerInterface::RETURN_ONLY_ACTIVE); + + list($entity_position, $current_user_position) = $get_plugins_location(array_keys($plugins)); + $this->assertTrue($current_user_position > $entity_position); + } + +} From 6e0b1e52c6a1634dbfeab05e45a71e482837ee79 Mon Sep 17 00:00:00 2001 From: Roy Segall Date: Mon, 25 Jul 2016 00:13:40 +0300 Subject: [PATCH 16/32] CS fixes(hope) --- src/Entity/OgContextNegotiation.php | 6 +++--- src/Og.php | 1 + src/OgContextHandler.php | 14 ++++++++++++-- src/OgContextHandlerInterface.php | 23 ++++++++++++++++++----- src/OgContextInterface.php | 9 +++++++-- src/OgContextNegotiationInterface.php | 4 ++-- src/Plugin/OgContext/CurrentUser.php | 6 ++++++ src/Plugin/OgContext/Entity.php | 7 +++++++ tests/src/Kernel/Entity/OgContextTest.php | 18 ++++++------------ 9 files changed, 62 insertions(+), 26 deletions(-) diff --git a/src/Entity/OgContextNegotiation.php b/src/Entity/OgContextNegotiation.php index ed77b295d..bc471f84a 100644 --- a/src/Entity/OgContextNegotiation.php +++ b/src/Entity/OgContextNegotiation.php @@ -1,12 +1,12 @@ get('weight') > $og_context_config[$b['id']]->get('weight') ? 1 : -1; }); } @@ -132,7 +142,7 @@ public function updateConfigStorage() { continue; } - // Registering a new negotiation plugin.s + // Registering a new negotiation plugin. $og_context_storage->create([ 'id' => $plugin['id'], 'label' => $plugin['label'], diff --git a/src/OgContextHandlerInterface.php b/src/OgContextHandlerInterface.php index 725aa237d..4a48a4dee 100644 --- a/src/OgContextHandlerInterface.php +++ b/src/OgContextHandlerInterface.php @@ -2,8 +2,17 @@ namespace Drupal\og; +/** + * @file + * Contains \Drupal\og\OgContextHandlerInterface. + */ + use Drupal\Core\Entity\ContentEntityBase; +/** + * Interface OgContextHandlerInterface + * @package Drupal\og + */ interface OgContextHandlerInterface { /** @@ -12,8 +21,7 @@ interface OgContextHandlerInterface { const RETURN_ONLY_ACTIVE = 1; /** - * Return only plugins which stored in he negotiation schema with a status of - * enabled/disabled. + * Return only plugins which stored in the negotiation schema. */ const RETURN_ONLY_IN_STORAGE = 2; @@ -25,7 +33,8 @@ interface OgContextHandlerInterface { /** * Get the current viewed group. * - * @return NULL|ContentEntityBase + * @return ContentEntityBase + * Return the group(s) object(s) which match the log of of the plugin. */ public function getGroup(); @@ -46,29 +55,33 @@ public function getGroup(); * OgContextHandlerInterface::RETURN_ONLY_IN_STORAGE - will return only * plugins with a corresponding OG context negotiation entity entry no * matter what is the status. + * * @return array + * List of OG context plugins. */ public function getPlugins($return_mode); /** * Create an instance of an OG context plugin. * - * @param $plugin_id + * @param string $plugin_id * The plugin ID. * * @return OgContextBase + * An OG context plugins instance. */ public function getPlugin($plugin_id); /** * Update plugin settings. * - * @param $plugin_id + * @param string $plugin_id * The plugin ID. * @param array $config * The plugins settings. * * @return bool + * True or false of the update has succeeded. */ public function updatePlugin($plugin_id, $config = []); diff --git a/src/OgContextInterface.php b/src/OgContextInterface.php index b52d3ab93..cdefe37a2 100644 --- a/src/OgContextInterface.php +++ b/src/OgContextInterface.php @@ -2,8 +2,12 @@ namespace Drupal\og; -use Drupal\Component\Plugin\PluginInspectionInterface; +/** + * @file + * Contains \Drupal\og\OgContextInterface. + */ +use Drupal\Component\Plugin\PluginInspectionInterface; use Drupal\Core\Entity\ContentEntityBase; /** @@ -14,7 +18,8 @@ interface OgContextInterface extends PluginInspectionInterface { /** * Return the current group. If no group will be found return Null. * - * @return NULL|ContentEntityBase + * @return ContentEntityBase + * Return the best matching group. */ public function getGroup(); diff --git a/src/OgContextNegotiationInterface.php b/src/OgContextNegotiationInterface.php index ff8260460..03f08a51d 100644 --- a/src/OgContextNegotiationInterface.php +++ b/src/OgContextNegotiationInterface.php @@ -1,12 +1,12 @@ getPlugins(OgContextHandlerInterface::RETURN_ALL); // Check we got the plugins we know that exists. @@ -85,8 +79,8 @@ function testOgContextPluginsList() { $this->assertArrayHasKey('entity', $plugins); $this->assertArrayHasKey('current_user', $plugins); - $get_plugins_location = function($plugins) { - return[ + $get_plugins_location = function ($plugins) { + return [ array_search('entity', $plugins), array_search('current_user', $plugins), ]; From c00439bed8300e6c644b31b43c4b3e05c4e45202 Mon Sep 17 00:00:00 2001 From: Roy Segall Date: Mon, 25 Jul 2016 00:24:44 +0300 Subject: [PATCH 17/32] More CS. --- src/OgContextHandler.php | 2 +- src/OgContextHandlerInterface.php | 1 + src/Plugin/OgContext/CurrentUser.php | 2 ++ src/Plugin/OgContext/Entity.php | 1 + tests/src/Kernel/Entity/OgContextTest.php | 2 +- 5 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/OgContextHandler.php b/src/OgContextHandler.php index a9f31c7cb..ffbe7a927 100644 --- a/src/OgContextHandler.php +++ b/src/OgContextHandler.php @@ -12,7 +12,7 @@ use Drupal\og\Entity\OgContextNegotiation; /** - * Class OgContextHandler + * Class OgContextHandler. * * @package Drupal\og */ diff --git a/src/OgContextHandlerInterface.php b/src/OgContextHandlerInterface.php index 4a48a4dee..37145d187 100644 --- a/src/OgContextHandlerInterface.php +++ b/src/OgContextHandlerInterface.php @@ -11,6 +11,7 @@ /** * Interface OgContextHandlerInterface + * * @package Drupal\og */ interface OgContextHandlerInterface { diff --git a/src/Plugin/OgContext/CurrentUser.php b/src/Plugin/OgContext/CurrentUser.php index 11457ac94..2fae86732 100644 --- a/src/Plugin/OgContext/CurrentUser.php +++ b/src/Plugin/OgContext/CurrentUser.php @@ -12,6 +12,8 @@ use Symfony\Component\DependencyInjection\ContainerInterface; /** + * Get the group from the current logged in user. + * * @OgContext( * id = "current_user", * label = "Current user", diff --git a/src/Plugin/OgContext/Entity.php b/src/Plugin/OgContext/Entity.php index 310a873fb..edde7462c 100644 --- a/src/Plugin/OgContext/Entity.php +++ b/src/Plugin/OgContext/Entity.php @@ -15,6 +15,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface; /** + * Get the group from the given viewed entity. * @OgContext( * id = "entity", * label = "Entity", diff --git a/tests/src/Kernel/Entity/OgContextTest.php b/tests/src/Kernel/Entity/OgContextTest.php index 993d0d244..8ba398319 100644 --- a/tests/src/Kernel/Entity/OgContextTest.php +++ b/tests/src/Kernel/Entity/OgContextTest.php @@ -33,7 +33,7 @@ class OgContextTest extends KernelTestBase { /** * {@inheritdoc} */ - function setUp() { + public function setUp() { parent::setUp(); $this->installConfig(['og']); From 245cc8a6e5120d98f510666613fd7ca8a62245b5 Mon Sep 17 00:00:00 2001 From: Roy Segall Date: Mon, 25 Jul 2016 00:32:19 +0300 Subject: [PATCH 18/32] she's into a typical riff. --- src/OgContextHandlerInterface.php | 2 +- src/Plugin/OgContext/Entity.php | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/OgContextHandlerInterface.php b/src/OgContextHandlerInterface.php index 37145d187..26ac169f4 100644 --- a/src/OgContextHandlerInterface.php +++ b/src/OgContextHandlerInterface.php @@ -10,7 +10,7 @@ use Drupal\Core\Entity\ContentEntityBase; /** - * Interface OgContextHandlerInterface + * Interface OgContextHandlerInterface. * * @package Drupal\og */ diff --git a/src/Plugin/OgContext/Entity.php b/src/Plugin/OgContext/Entity.php index edde7462c..d05b91c53 100644 --- a/src/Plugin/OgContext/Entity.php +++ b/src/Plugin/OgContext/Entity.php @@ -16,6 +16,7 @@ /** * Get the group from the given viewed entity. + * * @OgContext( * id = "entity", * label = "Entity", From ddb9d7f847830035658b48e6e699cf8d4ef9e7cd Mon Sep 17 00:00:00 2001 From: Roy Segall Date: Mon, 25 Jul 2016 00:38:41 +0300 Subject: [PATCH 19/32] Hmm... --- src/OgContextHandler.php | 1 - src/OgContextHandlerInterface.php | 2 -- src/OgContextInterface.php | 1 - 3 files changed, 4 deletions(-) diff --git a/src/OgContextHandler.php b/src/OgContextHandler.php index ffbe7a927..c32be3d95 100644 --- a/src/OgContextHandler.php +++ b/src/OgContextHandler.php @@ -9,7 +9,6 @@ use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; -use Drupal\og\Entity\OgContextNegotiation; /** * Class OgContextHandler. diff --git a/src/OgContextHandlerInterface.php b/src/OgContextHandlerInterface.php index 26ac169f4..263c2cd4c 100644 --- a/src/OgContextHandlerInterface.php +++ b/src/OgContextHandlerInterface.php @@ -7,8 +7,6 @@ * Contains \Drupal\og\OgContextHandlerInterface. */ -use Drupal\Core\Entity\ContentEntityBase; - /** * Interface OgContextHandlerInterface. * diff --git a/src/OgContextInterface.php b/src/OgContextInterface.php index cdefe37a2..1c5a84482 100644 --- a/src/OgContextInterface.php +++ b/src/OgContextInterface.php @@ -8,7 +8,6 @@ */ use Drupal\Component\Plugin\PluginInspectionInterface; -use Drupal\Core\Entity\ContentEntityBase; /** * Defines an interface for OG context plugins. From ec3f732e0bc3bed066ab9aa07f7eea5bc2029d13 Mon Sep 17 00:00:00 2001 From: RoySegall Date: Tue, 26 Jul 2016 15:59:10 +0300 Subject: [PATCH 20/32] Return the first group for now. --- src/OgContextHandler.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/OgContextHandler.php b/src/OgContextHandler.php index c32be3d95..32a7be3cd 100644 --- a/src/OgContextHandler.php +++ b/src/OgContextHandler.php @@ -60,13 +60,17 @@ public function __construct(ConfigFactoryInterface $config_factory, OgContextMan public function getGroup() { $plugins = $this->getPlugins(); + $groups = []; + foreach ($plugins as $plugin) { if ($group = $this->getPlugin($plugin['id'])->getGroup()) { - return $group; + $groups = array_merge($groups, $group); } } - return NULL; + // Return the first group for now. handle in a follow up PR to find the best + // matching group. + return reset($groups); } /** From cb4bf5a76a06bd74d100ed6ce43c1ed825046847 Mon Sep 17 00:00:00 2001 From: Roy Segall Date: Wed, 27 Jul 2016 21:29:31 +0300 Subject: [PATCH 21/32] Keep entity as the only plugin. --- .../Plugin/OgContext/UserGroupReference.php | 29 +++++++++++++++++++ tests/src/Kernel/Entity/OgContextTest.php | 22 +++++++------- 2 files changed, 40 insertions(+), 11 deletions(-) create mode 100644 tests/modules/og_test/src/Plugin/OgContext/UserGroupReference.php diff --git a/tests/modules/og_test/src/Plugin/OgContext/UserGroupReference.php b/tests/modules/og_test/src/Plugin/OgContext/UserGroupReference.php new file mode 100644 index 000000000..499f355d8 --- /dev/null +++ b/tests/modules/og_test/src/Plugin/OgContext/UserGroupReference.php @@ -0,0 +1,29 @@ +getPlugins(OgContextHandlerInterface::RETURN_ALL); // Check we got the plugins we know that exists. - if (empty($plugins['current_user']) || empty($plugins['entity'])) { + if (empty($plugins['user_group_reference']) || empty($plugins['entity'])) { $this->fail('The expected plugins, Current user and Entity, was not found in the plugins list'); } // Get all the plugins in the storage which have a matching storage. $plugins = Og::contextHandler()->getPlugins(OgContextHandlerInterface::RETURN_ONLY_IN_STORAGE); - if (empty($plugins['current_user']) || empty($plugins['entity'])) { + if (empty($plugins['user_group_reference']) || empty($plugins['entity'])) { $this->fail('The expected plugins, Current user and Entity, was not found in the plugins list'); } @@ -69,34 +69,34 @@ public function testOgContextPluginsList() { $plugins = Og::contextHandler()->getPlugins(OgContextHandlerInterface::RETURN_ONLY_ACTIVE); $this->assertArrayHasKey('entity', $plugins); - $this->assertArrayNotHasKey('current_user', $plugins); + $this->assertArrayNotHasKey('user_group_reference', $plugins); // Enable the second plugin and change the weight. Og::contextHandler()->updatePlugin('entity', ['weight' => 1]); - Og::contextHandler()->updatePlugin('current_user', ['weight' => 0, 'status' => 1]); + Og::contextHandler()->updatePlugin('user_group_reference', ['weight' => 0, 'status' => 1]); $plugins = Og::contextHandler()->getPlugins(OgContextHandlerInterface::RETURN_ONLY_ACTIVE); $this->assertArrayHasKey('entity', $plugins); - $this->assertArrayHasKey('current_user', $plugins); + $this->assertArrayHasKey('user_group_reference', $plugins); $get_plugins_location = function ($plugins) { return [ array_search('entity', $plugins), - array_search('current_user', $plugins), + array_search('user_group_reference', $plugins), ]; }; - list($entity_position, $current_user_position) = $get_plugins_location(array_keys($plugins)); + list($entity_position, $user_group_reference_position) = $get_plugins_location(array_keys($plugins)); - $this->assertTrue($entity_position > $current_user_position); + $this->assertTrue($entity_position > $user_group_reference_position); // Change the weight. Og::contextHandler()->updatePlugin('entity', ['weight' => -1]); $plugins = Og::contextHandler()->getPlugins(OgContextHandlerInterface::RETURN_ONLY_ACTIVE); - list($entity_position, $current_user_position) = $get_plugins_location(array_keys($plugins)); - $this->assertTrue($current_user_position > $entity_position); + list($entity_position, $user_group_reference_position) = $get_plugins_location(array_keys($plugins)); + $this->assertTrue($user_group_reference_position > $entity_position); } } From 3a6d73e3e973f4fdcb04b2d6f0413d7da1feda06 Mon Sep 17 00:00:00 2001 From: Roy Segall Date: Wed, 27 Jul 2016 22:51:53 +0300 Subject: [PATCH 22/32] Testing the context it self. --- src/OgContextHandler.php | 2 +- src/Plugin/OgContext/CurrentUser.php | 67 ----------- .../Functional/OgContextRightContextTest.php | 104 ++++++++++++++++++ tests/src/Kernel/Entity/OgContextTest.php | 5 - 4 files changed, 105 insertions(+), 73 deletions(-) delete mode 100644 src/Plugin/OgContext/CurrentUser.php create mode 100644 tests/src/Functional/OgContextRightContextTest.php diff --git a/src/OgContextHandler.php b/src/OgContextHandler.php index 32a7be3cd..44148b2ff 100644 --- a/src/OgContextHandler.php +++ b/src/OgContextHandler.php @@ -133,7 +133,7 @@ public function updatePlugin($plugin_id, $config = []) { * {@inheritdoc} */ public function updateConfigStorage() { - $plugins = $this->getPlugins(['return_mode' => OgContextHandlerInterface::RETURN_ALL]); + $plugins = $this->getPlugins(OgContextHandlerInterface::RETURN_ALL); $og_context_storage = $this->storage; $og_context_config = $og_context_storage->loadMultiple(); diff --git a/src/Plugin/OgContext/CurrentUser.php b/src/Plugin/OgContext/CurrentUser.php deleted file mode 100644 index 2fae86732..000000000 --- a/src/Plugin/OgContext/CurrentUser.php +++ /dev/null @@ -1,67 +0,0 @@ -currentUser = $current_user; - } - - /** - * {@inheritdoc} - */ - public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { - return new static( - $configuration, - $plugin_id, - $plugin_definition, - $container->get('current_user') - ); - } - - /** - * {@inheritdoc} - */ - public function getGroup() { - } - -} diff --git a/tests/src/Functional/OgContextRightContextTest.php b/tests/src/Functional/OgContextRightContextTest.php new file mode 100644 index 000000000..53400e640 --- /dev/null +++ b/tests/src/Functional/OgContextRightContextTest.php @@ -0,0 +1,104 @@ +updateConfigStorage(); + Og::createField(OgGroupAudienceHelper::DEFAULT_FIELD, 'user', 'user'); + + $group_type = NodeType::create([ + 'type' => 'group', + 'name' => $this->randomString(), + ]); + $group_type->save(); + Og::groupManager()->addGroup('node', $group_type->id()); + + // Create the user and populate the field. + $this->user = $this->drupalCreateUser(['access content']); + + // Create the groups. + $this->group1 = Node::create([ + 'title' => $this->randomString(), + 'type' => $group_type->id(), + 'uid' => $this->user->id(), + ]); + $this->group1->save(); + + $this->group2 = Node::create([ + 'title' => $this->randomString(), + 'type' => $group_type->id(), + 'uid' => $this->user->id(), + ]); + $this->group2->save(); + + Og::contextHandler()->updatePlugin('entity', ['status' => 1, 'weight' => 0]); + Og::contextHandler()->updatePlugin('user_group_reference', ['status' => 1, 'weight' => 1]); + } + + /** + * Verify the context handler will return the correct OG context. + */ + public function testOgContextPluginsList() { + $this->drupalGet('node/' . $this->group1->id()); + } + +} diff --git a/tests/src/Kernel/Entity/OgContextTest.php b/tests/src/Kernel/Entity/OgContextTest.php index 2293de821..24e0eb767 100644 --- a/tests/src/Kernel/Entity/OgContextTest.php +++ b/tests/src/Kernel/Entity/OgContextTest.php @@ -10,8 +10,6 @@ use Drupal\KernelTests\KernelTestBase; use Drupal\og\Og; use Drupal\og\OgContextHandlerInterface; -use Drupal\simpletest\ContentTypeCreationTrait; -use Drupal\simpletest\NodeCreationTrait; /** * Test OG context plugins. @@ -22,9 +20,6 @@ */ class OgContextTest extends KernelTestBase { - use ContentTypeCreationTrait; - use NodeCreationTrait; - /** * {@inheritdoc} */ From 17538870b98ca063a8fc8b68a2af3912d0533738 Mon Sep 17 00:00:00 2001 From: Roy Segall Date: Sat, 6 Aug 2016 14:14:07 +0300 Subject: [PATCH 23/32] Rename the plugin to GroupResolver. --- og.services.yml | 2 +- .../{OgContext.php => GroupResolver.php} | 2 +- ...xtManager.php => GroupResolverManager.php} | 10 +- src/OgContextHandler.php | 6 +- .../{OgContext => GroupResolver}/Entity.php | 6 +- .../UserGroupReference.php | 6 +- .../Functional/OgContextRightContextTest.php | 104 ------------------ 7 files changed, 16 insertions(+), 120 deletions(-) rename src/Annotation/{OgContext.php => GroupResolver.php} (92%) rename src/{OgContextManager.php => GroupResolverManager.php} (68%) rename src/Plugin/{OgContext => GroupResolver}/Entity.php (95%) rename tests/modules/og_test/src/Plugin/{OgContext => GroupResolver}/UserGroupReference.php (76%) delete mode 100644 tests/src/Functional/OgContextRightContextTest.php diff --git a/og.services.yml b/og.services.yml index 2bb3101c2..4d702abe8 100644 --- a/og.services.yml +++ b/og.services.yml @@ -26,5 +26,5 @@ services: class: Drupal\og\OgFieldsPluginManager parent: default_plugin_manager plugin.manager.og.context: - class: Drupal\og\OgContextManager + class: Drupal\og\GroupResolverManager parent: default_plugin_manager diff --git a/src/Annotation/OgContext.php b/src/Annotation/GroupResolver.php similarity index 92% rename from src/Annotation/OgContext.php rename to src/Annotation/GroupResolver.php index 00a0705f6..47472bb0b 100644 --- a/src/Annotation/OgContext.php +++ b/src/Annotation/GroupResolver.php @@ -12,7 +12,7 @@ * * @Annotation */ -class OgContext extends Plugin { +class GroupResolver extends Plugin { /** * The plugin ID. diff --git a/src/OgContextManager.php b/src/GroupResolverManager.php similarity index 68% rename from src/OgContextManager.php rename to src/GroupResolverManager.php index d7d20a027..d0a65309b 100644 --- a/src/OgContextManager.php +++ b/src/GroupResolverManager.php @@ -9,10 +9,10 @@ /** * Provides the OG context plugin manager. */ -class OgContextManager extends DefaultPluginManager { +class GroupResolverManager extends DefaultPluginManager { /** - * Constructor for OgContextManager objects. + * Constructor for GroupResolverManager objects. * * @param \Traversable $namespaces * An object that implements \Traversable which contains the root paths @@ -23,10 +23,10 @@ class OgContextManager extends DefaultPluginManager { * The module handler to invoke the alter hook with. */ public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) { - parent::__construct('Plugin/OgContext', $namespaces, $module_handler, 'Drupal\og\OgContextInterface', 'Drupal\og\Annotation\OgContext'); + parent::__construct('Plugin/GroupResolver', $namespaces, $module_handler, 'Drupal\og\OgContextInterface', 'Drupal\og\Annotation\GroupResolver'); - $this->alterInfo('og_context_og_context_info'); - $this->setCacheBackend($cache_backend, 'og_context_og_context_plugins'); + $this->alterInfo('og_group_resolver_info'); + $this->setCacheBackend($cache_backend, 'og_group_resolver_plugins'); } } diff --git a/src/OgContextHandler.php b/src/OgContextHandler.php index 44148b2ff..ba0be28b4 100644 --- a/src/OgContextHandler.php +++ b/src/OgContextHandler.php @@ -27,7 +27,7 @@ class OgContextHandler implements OgContextHandlerInterface { /** * The OG context manager. * - * @var \Drupal\og\OgContextManager + * @var \Drupal\og\GroupResolverManager */ protected $pluginManager; @@ -43,12 +43,12 @@ class OgContextHandler implements OgContextHandlerInterface { * * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory * The config factory. - * @param \Drupal\og\OgContextManager $context_manager + * @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, OgContextManager $context_manager, EntityTypeManagerInterface $entity_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('og_context_negotiation'); diff --git a/src/Plugin/OgContext/Entity.php b/src/Plugin/GroupResolver/Entity.php similarity index 95% rename from src/Plugin/OgContext/Entity.php rename to src/Plugin/GroupResolver/Entity.php index d05b91c53..852b37db0 100644 --- a/src/Plugin/OgContext/Entity.php +++ b/src/Plugin/GroupResolver/Entity.php @@ -1,10 +1,10 @@ updateConfigStorage(); - Og::createField(OgGroupAudienceHelper::DEFAULT_FIELD, 'user', 'user'); - - $group_type = NodeType::create([ - 'type' => 'group', - 'name' => $this->randomString(), - ]); - $group_type->save(); - Og::groupManager()->addGroup('node', $group_type->id()); - - // Create the user and populate the field. - $this->user = $this->drupalCreateUser(['access content']); - - // Create the groups. - $this->group1 = Node::create([ - 'title' => $this->randomString(), - 'type' => $group_type->id(), - 'uid' => $this->user->id(), - ]); - $this->group1->save(); - - $this->group2 = Node::create([ - 'title' => $this->randomString(), - 'type' => $group_type->id(), - 'uid' => $this->user->id(), - ]); - $this->group2->save(); - - Og::contextHandler()->updatePlugin('entity', ['status' => 1, 'weight' => 0]); - Og::contextHandler()->updatePlugin('user_group_reference', ['status' => 1, 'weight' => 1]); - } - - /** - * Verify the context handler will return the correct OG context. - */ - public function testOgContextPluginsList() { - $this->drupalGet('node/' . $this->group1->id()); - } - -} From 603fffe7fb2ea60b3640ee68e6ac50eddfccccf5 Mon Sep 17 00:00:00 2001 From: Roy Segall Date: Sat, 6 Aug 2016 19:47:05 +0300 Subject: [PATCH 24/32] Refactor the entity name [skip ci] --- ... => group_resolver_negotiation.schema.yml} | 2 +- src/Annotation/GroupResolver.php | 2 +- ...ation.php => GroupResolverNegotiation.php} | 14 +++++------ ... => GroupResolverNegotiationInterface.php} | 4 ++-- src/OgContextHandler.php | 23 ++++++++++--------- 5 files changed, 23 insertions(+), 22 deletions(-) rename config/schema/{og_context_negotiation.schema.yml => group_resolver_negotiation.schema.yml} (91%) rename src/Entity/{OgContextNegotiation.php => GroupResolverNegotiation.php} (66%) rename src/{OgContextNegotiationInterface.php => GroupResolverNegotiationInterface.php} (57%) diff --git a/config/schema/og_context_negotiation.schema.yml b/config/schema/group_resolver_negotiation.schema.yml similarity index 91% rename from config/schema/og_context_negotiation.schema.yml rename to config/schema/group_resolver_negotiation.schema.yml index 81c256ea3..78ac7765b 100644 --- a/config/schema/og_context_negotiation.schema.yml +++ b/config/schema/group_resolver_negotiation.schema.yml @@ -1,4 +1,4 @@ -og.og_context_negotiation.*: +og.group_resolver_negotiation.*: type: config_entity label: 'OG context negotiation config' mapping: diff --git a/src/Annotation/GroupResolver.php b/src/Annotation/GroupResolver.php index 47472bb0b..ebbc906ae 100644 --- a/src/Annotation/GroupResolver.php +++ b/src/Annotation/GroupResolver.php @@ -7,7 +7,7 @@ /** * Defines a OG context item annotation object. * - * @see \Drupal\og_context\Plugin\OgContextManager + * @see \Drupal\og\Plugin\OgContextManager * @see plugin_api * * @Annotation diff --git a/src/Entity/OgContextNegotiation.php b/src/Entity/GroupResolverNegotiation.php similarity index 66% rename from src/Entity/OgContextNegotiation.php rename to src/Entity/GroupResolverNegotiation.php index bc471f84a..ff6753090 100644 --- a/src/Entity/OgContextNegotiation.php +++ b/src/Entity/GroupResolverNegotiation.php @@ -8,15 +8,15 @@ */ use Drupal\Core\Config\Entity\ConfigEntityBase; -use Drupal\og\OgContextNegotiationInterface; +use Drupal\og\GroupResolverNegotiationInterface; /** * Defines the OG context negotiation entity. * * @ConfigEntityType( - * id = "og_context_negotiation", - * label = @Translation("OG context negotiation"), - * config_prefix = "og_context_negotiation", + * id = "group_resolver_negotiation", + * label = @Translation("OG group resolver negotiation"), + * config_prefix = "group_resolver_negotiation", * admin_permission = "administer site configuration", * entity_keys = { * "id" = "id", @@ -25,17 +25,17 @@ * } * ) */ -class OgContextNegotiation extends ConfigEntityBase implements OgContextNegotiationInterface { +class GroupResolverNegotiation extends ConfigEntityBase implements GroupResolverNegotiationInterface { /** - * The OG context negotiation ID. + * The OG group resolver negotiation ID. * * @var string */ protected $id; /** - * The OG context negotiation label. + * The OG group resolver negotiation label. * * @var string */ diff --git a/src/OgContextNegotiationInterface.php b/src/GroupResolverNegotiationInterface.php similarity index 57% rename from src/OgContextNegotiationInterface.php rename to src/GroupResolverNegotiationInterface.php index 03f08a51d..17b3080d1 100644 --- a/src/OgContextNegotiationInterface.php +++ b/src/GroupResolverNegotiationInterface.php @@ -4,7 +4,7 @@ /** * @file - * Contains Drupal\og\OgContextNegotiationInterface. + * Contains Drupal\og\GroupResolverNegotiationInterface. */ use Drupal\Core\Config\Entity\ConfigEntityInterface; @@ -12,6 +12,6 @@ /** * Provides an interface for defining OG context negotiation entities. */ -interface OgContextNegotiationInterface extends ConfigEntityInterface { +interface GroupResolverNegotiationInterface extends ConfigEntityInterface { } diff --git a/src/OgContextHandler.php b/src/OgContextHandler.php index ba0be28b4..8d86ce96f 100644 --- a/src/OgContextHandler.php +++ b/src/OgContextHandler.php @@ -9,6 +9,7 @@ use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; +use Drupal\og\Entity\GroupResolverNegotiation; /** * Class OgContextHandler. @@ -51,7 +52,7 @@ class OgContextHandler implements OgContextHandlerInterface { 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('og_context_negotiation'); + $this->storage = $entity_manager->getStorage('group_resolver_negotiation'); } /** @@ -78,14 +79,14 @@ public function getGroup() { */ public function getPlugins($return_mode = OgContextHandlerInterface::RETURN_ONLY_ACTIVE) { - /** @var OgContextNegotiation[] $og_context_config */ - $og_context_config = $this->storage->loadMultiple(); + /** @var GroupResolverNegotiation[] $group_resolver_config */ + $group_resolver_config = $this->storage->loadMultiple(); $plugins = $this->pluginManager->getDefinitions(); if ($return_mode != OgContextHandlerInterface::RETURN_ALL) { - foreach ($og_context_config as $context) { + foreach ($group_resolver_config as $context) { if ($return_mode == OgContextHandlerInterface::RETURN_ONLY_ACTIVE) { $condition = $context->get('status') == FALSE; } @@ -99,9 +100,9 @@ public function getPlugins($return_mode = OgContextHandlerInterface::RETURN_ONLY } } - if (!empty($og_context_config)) { - uasort($plugins, function ($a, $b) use ($og_context_config) { - return $og_context_config[$a['id']]->get('weight') > $og_context_config[$b['id']]->get('weight') ? 1 : -1; + 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; }); } @@ -135,18 +136,18 @@ public function updatePlugin($plugin_id, $config = []) { public function updateConfigStorage() { $plugins = $this->getPlugins(OgContextHandlerInterface::RETURN_ALL); - $og_context_storage = $this->storage; - $og_context_config = $og_context_storage->loadMultiple(); + $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($og_context_config))) { + if (in_array($plugin['id'], array_keys($group_resolver_config))) { // The negotiation plugin already registered. continue; } // Registering a new negotiation plugin. - $og_context_storage->create([ + $group_resolver_storage->create([ 'id' => $plugin['id'], 'label' => $plugin['label'], 'description' => $plugin['description'], From 113b402e4746d2bf53d949fcb538b0944c631dc5 Mon Sep 17 00:00:00 2001 From: Roy Segall Date: Sat, 6 Aug 2016 19:55:46 +0300 Subject: [PATCH 25/32] More renaming [skip ci] --- og.services.yml | 4 ++-- ...ContextHandler.php => GroupResolverHandler.php} | 2 +- ...rface.php => GroupResolverHandlerInterface.php} | 2 +- src/Og.php | 4 ++-- tests/src/Kernel/Entity/OgContextTest.php | 14 +++++++------- 5 files changed, 13 insertions(+), 13 deletions(-) rename src/{OgContextHandler.php => GroupResolverHandler.php} (98%) rename src/{OgContextHandlerInterface.php => GroupResolverHandlerInterface.php} (98%) diff --git a/og.services.yml b/og.services.yml index 4d702abe8..156cb17bf 100644 --- a/og.services.yml +++ b/og.services.yml @@ -16,8 +16,8 @@ services: og.permission_manager: class: Drupal\og\PermissionManager arguments: ['@event_dispatcher'] - og.context_handler: - class: Drupal\og\OgContextHandler + og.group_resolver: + class: Drupal\og\GroupResolverHandler arguments: ['@config.factory', '@plugin.manager.og.context', '@entity_type.manager'] plugin.manager.og.delete_orphans: class: Drupal\og\OgDeleteOrphansPluginManager diff --git a/src/OgContextHandler.php b/src/GroupResolverHandler.php similarity index 98% rename from src/OgContextHandler.php rename to src/GroupResolverHandler.php index 8d86ce96f..893f282cf 100644 --- a/src/OgContextHandler.php +++ b/src/GroupResolverHandler.php @@ -16,7 +16,7 @@ * * @package Drupal\og */ -class OgContextHandler implements OgContextHandlerInterface { +class GroupResolverHandler implements OgContextHandlerInterface { /** * The config factory. diff --git a/src/OgContextHandlerInterface.php b/src/GroupResolverHandlerInterface.php similarity index 98% rename from src/OgContextHandlerInterface.php rename to src/GroupResolverHandlerInterface.php index 263c2cd4c..d3de40e66 100644 --- a/src/OgContextHandlerInterface.php +++ b/src/GroupResolverHandlerInterface.php @@ -12,7 +12,7 @@ * * @package Drupal\og */ -interface OgContextHandlerInterface { +interface GroupResolverHandlerInterface { /** * Return only plugins which active in the negotiation schema. diff --git a/src/Og.php b/src/Og.php index 6bff32a71..e5077d4e5 100644 --- a/src/Og.php +++ b/src/Og.php @@ -604,11 +604,11 @@ public static function permissionHandler() { /** * Return the og context handler instance. * - * @return OgContextHandler + * @return GroupResolverHandler * The OG context handler. */ public static function contextHandler() { - return \Drupal::service('og.context_handler'); + return \Drupal::service('og.group_resolver'); } /** diff --git a/tests/src/Kernel/Entity/OgContextTest.php b/tests/src/Kernel/Entity/OgContextTest.php index 24e0eb767..e52754e2b 100644 --- a/tests/src/Kernel/Entity/OgContextTest.php +++ b/tests/src/Kernel/Entity/OgContextTest.php @@ -9,7 +9,7 @@ use Drupal\KernelTests\KernelTestBase; use Drupal\og\Og; -use Drupal\og\OgContextHandlerInterface; +use Drupal\og\GroupResolverHandlerInterface; /** * Test OG context plugins. @@ -40,7 +40,7 @@ public function setUp() { * Test that OgContext handler will return a list according to a given logic. */ public function testOgContextPluginsList() { - $plugins = Og::contextHandler()->getPlugins(OgContextHandlerInterface::RETURN_ALL); + $plugins = Og::contextHandler()->getPlugins(GroupResolverHandlerInterface::RETURN_ALL); // Check we got the plugins we know that exists. if (empty($plugins['user_group_reference']) || empty($plugins['entity'])) { @@ -48,21 +48,21 @@ public function testOgContextPluginsList() { } // Get all the plugins in the storage which have a matching storage. - $plugins = Og::contextHandler()->getPlugins(OgContextHandlerInterface::RETURN_ONLY_IN_STORAGE); + $plugins = Og::contextHandler()->getPlugins(GroupResolverHandlerInterface::RETURN_ONLY_IN_STORAGE); if (empty($plugins['user_group_reference']) || empty($plugins['entity'])) { $this->fail('The expected plugins, Current user and Entity, was not found in the plugins list'); } // Get all active plugins. - $plugins = Og::contextHandler()->getPlugins(OgContextHandlerInterface::RETURN_ONLY_ACTIVE); + $plugins = Og::contextHandler()->getPlugins(GroupResolverHandlerInterface::RETURN_ONLY_ACTIVE); $this->assertEmpty($plugins); // Enable a plugin. Og::contextHandler()->updatePlugin('entity', ['status' => 1]); - $plugins = Og::contextHandler()->getPlugins(OgContextHandlerInterface::RETURN_ONLY_ACTIVE); + $plugins = Og::contextHandler()->getPlugins(GroupResolverHandlerInterface::RETURN_ONLY_ACTIVE); $this->assertArrayHasKey('entity', $plugins); $this->assertArrayNotHasKey('user_group_reference', $plugins); @@ -70,7 +70,7 @@ public function testOgContextPluginsList() { Og::contextHandler()->updatePlugin('entity', ['weight' => 1]); Og::contextHandler()->updatePlugin('user_group_reference', ['weight' => 0, 'status' => 1]); - $plugins = Og::contextHandler()->getPlugins(OgContextHandlerInterface::RETURN_ONLY_ACTIVE); + $plugins = Og::contextHandler()->getPlugins(GroupResolverHandlerInterface::RETURN_ONLY_ACTIVE); $this->assertArrayHasKey('entity', $plugins); $this->assertArrayHasKey('user_group_reference', $plugins); @@ -88,7 +88,7 @@ public function testOgContextPluginsList() { // Change the weight. Og::contextHandler()->updatePlugin('entity', ['weight' => -1]); - $plugins = Og::contextHandler()->getPlugins(OgContextHandlerInterface::RETURN_ONLY_ACTIVE); + $plugins = Og::contextHandler()->getPlugins(GroupResolverHandlerInterface::RETURN_ONLY_ACTIVE); list($entity_position, $user_group_reference_position) = $get_plugins_location(array_keys($plugins)); $this->assertTrue($user_group_reference_position > $entity_position); From f3c98a8edd19a07ec28bfc351bf3fbb46f7a5021 Mon Sep 17 00:00:00 2001 From: Roy Segall Date: Sat, 6 Aug 2016 20:13:02 +0300 Subject: [PATCH 26/32] More renaming [skip ci] --- src/GroupResolverHandler.php | 14 +++++++------- src/GroupResolverHandlerInterface.php | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/GroupResolverHandler.php b/src/GroupResolverHandler.php index 893f282cf..188b911dc 100644 --- a/src/GroupResolverHandler.php +++ b/src/GroupResolverHandler.php @@ -4,7 +4,7 @@ /** * @file - * Contains \Drupal\og\OgContextHandler. + * Contains \Drupal\og\GroupResolverHandler. */ use Drupal\Core\Config\ConfigFactoryInterface; @@ -16,7 +16,7 @@ * * @package Drupal\og */ -class GroupResolverHandler implements OgContextHandlerInterface { +class GroupResolverHandler implements GroupResolverHandlerInterface { /** * The config factory. @@ -77,17 +77,17 @@ public function getGroup() { /** * {@inheritdoc} */ - public function getPlugins($return_mode = OgContextHandlerInterface::RETURN_ONLY_ACTIVE) { + public function getPlugins($return_mode = GroupResolverHandlerInterface::RETURN_ONLY_ACTIVE) { /** @var GroupResolverNegotiation[] $group_resolver_config */ $group_resolver_config = $this->storage->loadMultiple(); $plugins = $this->pluginManager->getDefinitions(); - if ($return_mode != OgContextHandlerInterface::RETURN_ALL) { + if ($return_mode != GroupResolverHandlerInterface::RETURN_ALL) { foreach ($group_resolver_config as $context) { - if ($return_mode == OgContextHandlerInterface::RETURN_ONLY_ACTIVE) { + if ($return_mode == GroupResolverHandlerInterface::RETURN_ONLY_ACTIVE) { $condition = $context->get('status') == FALSE; } else { @@ -120,7 +120,7 @@ public function getPlugin($plugin_id) { * {@inheritdoc} */ public function updatePlugin($plugin_id, $config = []) { - /** @var OgContextNegotiation $contex */ + /** @var GroupResolverNegotiation $contex */ $context = $this->storage->load($plugin_id); foreach ($config as $key => $value) { @@ -134,7 +134,7 @@ public function updatePlugin($plugin_id, $config = []) { * {@inheritdoc} */ public function updateConfigStorage() { - $plugins = $this->getPlugins(OgContextHandlerInterface::RETURN_ALL); + $plugins = $this->getPlugins(GroupResolverHandlerInterface::RETURN_ALL); $group_resolver_storage = $this->storage; $group_resolver_config = $group_resolver_storage->loadMultiple(); diff --git a/src/GroupResolverHandlerInterface.php b/src/GroupResolverHandlerInterface.php index d3de40e66..85c589a7f 100644 --- a/src/GroupResolverHandlerInterface.php +++ b/src/GroupResolverHandlerInterface.php @@ -4,7 +4,7 @@ /** * @file - * Contains \Drupal\og\OgContextHandlerInterface. + * Contains \Drupal\og\GroupResolverHandlerInterface. */ /** From 0a0f5e449bda442a2404f14d6dbcec55b31c50c7 Mon Sep 17 00:00:00 2001 From: Roy Segall Date: Sat, 6 Aug 2016 20:48:59 +0300 Subject: [PATCH 27/32] Down with renaming. --- .../group_resolver_negotiation.schema.yml | 2 +- og.install | 2 +- og.module | 2 +- src/Annotation/GroupResolver.php | 4 ++-- src/Entity/GroupResolverNegotiation.php | 4 ++-- src/GroupResolverHandler.php | 6 ++--- src/GroupResolverManager.php | 2 +- src/GroupResolverNegotiationInterface.php | 2 +- src/Og.php | 2 +- tests/src/Kernel/Entity/OgContextTest.php | 22 +++++++++---------- 10 files changed, 24 insertions(+), 24 deletions(-) diff --git a/config/schema/group_resolver_negotiation.schema.yml b/config/schema/group_resolver_negotiation.schema.yml index 78ac7765b..9f0e818b3 100644 --- a/config/schema/group_resolver_negotiation.schema.yml +++ b/config/schema/group_resolver_negotiation.schema.yml @@ -1,6 +1,6 @@ og.group_resolver_negotiation.*: type: config_entity - label: 'OG context negotiation config' + label: 'OG group resolver negotiation config' mapping: id: type: string diff --git a/og.install b/og.install index ba5b0a0a2..bc606092b 100644 --- a/og.install +++ b/og.install @@ -79,5 +79,5 @@ function og_schema() { * Implements hook_install(). */ function og_install() { - \Drupal\og\Og::contextHandler()->updateConfigStorage(); + \Drupal\og\Og::groupResolverHandler()->updateConfigStorage(); } diff --git a/og.module b/og.module index 1cbd66373..4970c563f 100755 --- a/og.module +++ b/og.module @@ -208,7 +208,7 @@ function og_entity_create_access(AccountInterface $account, array $context, $bun * Implements hook_cache_flush(). */ function og_cache_flush() { - Og::contextHandler()->updateConfigStorage(); + Og::groupResolverHandler()->updateConfigStorage(); } /** diff --git a/src/Annotation/GroupResolver.php b/src/Annotation/GroupResolver.php index ebbc906ae..809ae1dac 100644 --- a/src/Annotation/GroupResolver.php +++ b/src/Annotation/GroupResolver.php @@ -5,9 +5,9 @@ use Drupal\Component\Annotation\Plugin; /** - * Defines a OG context item annotation object. + * Defines a OG group resolver item annotation object. * - * @see \Drupal\og\Plugin\OgContextManager + * @see \Drupal\og\Plugin\GroupResolverManager * @see plugin_api * * @Annotation diff --git a/src/Entity/GroupResolverNegotiation.php b/src/Entity/GroupResolverNegotiation.php index ff6753090..1f56b2a12 100644 --- a/src/Entity/GroupResolverNegotiation.php +++ b/src/Entity/GroupResolverNegotiation.php @@ -4,14 +4,14 @@ /** * @file - * Contains \Drupal\og\Entity\OgContextNegotiation. + * Contains \Drupal\og\Entity\GroupResolverNegotiation. */ use Drupal\Core\Config\Entity\ConfigEntityBase; use Drupal\og\GroupResolverNegotiationInterface; /** - * Defines the OG context negotiation entity. + * Defines the OG group resolver negotiation entity. * * @ConfigEntityType( * id = "group_resolver_negotiation", diff --git a/src/GroupResolverHandler.php b/src/GroupResolverHandler.php index 188b911dc..8626d56f8 100644 --- a/src/GroupResolverHandler.php +++ b/src/GroupResolverHandler.php @@ -12,7 +12,7 @@ use Drupal\og\Entity\GroupResolverNegotiation; /** - * Class OgContextHandler. + * Class GroupResolverHandler. * * @package Drupal\og */ @@ -26,7 +26,7 @@ class GroupResolverHandler implements GroupResolverHandlerInterface { protected $configFactory; /** - * The OG context manager. + * The OG group resolver manager. * * @var \Drupal\og\GroupResolverManager */ @@ -40,7 +40,7 @@ class GroupResolverHandler implements GroupResolverHandlerInterface { protected $storage; /** - * Constructs an OgManager service. + * Constructs an group resolver service. * * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory * The config factory. diff --git a/src/GroupResolverManager.php b/src/GroupResolverManager.php index d0a65309b..aaac58b46 100644 --- a/src/GroupResolverManager.php +++ b/src/GroupResolverManager.php @@ -7,7 +7,7 @@ use Drupal\Core\Extension\ModuleHandlerInterface; /** - * Provides the OG context plugin manager. + * Provides the OG group resolver plugin manager. */ class GroupResolverManager extends DefaultPluginManager { diff --git a/src/GroupResolverNegotiationInterface.php b/src/GroupResolverNegotiationInterface.php index 17b3080d1..3ebb8aa01 100644 --- a/src/GroupResolverNegotiationInterface.php +++ b/src/GroupResolverNegotiationInterface.php @@ -10,7 +10,7 @@ use Drupal\Core\Config\Entity\ConfigEntityInterface; /** - * Provides an interface for defining OG context negotiation entities. + * Provides an interface for defining OG group resolver negotiation entities. */ interface GroupResolverNegotiationInterface extends ConfigEntityInterface { diff --git a/src/Og.php b/src/Og.php index e5077d4e5..25f090b77 100644 --- a/src/Og.php +++ b/src/Og.php @@ -607,7 +607,7 @@ public static function permissionHandler() { * @return GroupResolverHandler * The OG context handler. */ - public static function contextHandler() { + public static function groupResolverHandler() { return \Drupal::service('og.group_resolver'); } diff --git a/tests/src/Kernel/Entity/OgContextTest.php b/tests/src/Kernel/Entity/OgContextTest.php index e52754e2b..c8cc20260 100644 --- a/tests/src/Kernel/Entity/OgContextTest.php +++ b/tests/src/Kernel/Entity/OgContextTest.php @@ -33,14 +33,14 @@ public function setUp() { $this->installConfig(['og']); - Og::contextHandler()->updateConfigStorage(); + Og::groupResolverHandler()->updateConfigStorage(); } /** * Test that OgContext handler will return a list according to a given logic. */ public function testOgContextPluginsList() { - $plugins = Og::contextHandler()->getPlugins(GroupResolverHandlerInterface::RETURN_ALL); + $plugins = Og::groupResolverHandler()->getPlugins(GroupResolverHandlerInterface::RETURN_ALL); // Check we got the plugins we know that exists. if (empty($plugins['user_group_reference']) || empty($plugins['entity'])) { @@ -48,29 +48,29 @@ public function testOgContextPluginsList() { } // Get all the plugins in the storage which have a matching storage. - $plugins = Og::contextHandler()->getPlugins(GroupResolverHandlerInterface::RETURN_ONLY_IN_STORAGE); + $plugins = Og::groupResolverHandler()->getPlugins(GroupResolverHandlerInterface::RETURN_ONLY_IN_STORAGE); if (empty($plugins['user_group_reference']) || empty($plugins['entity'])) { $this->fail('The expected plugins, Current user and Entity, was not found in the plugins list'); } // Get all active plugins. - $plugins = Og::contextHandler()->getPlugins(GroupResolverHandlerInterface::RETURN_ONLY_ACTIVE); + $plugins = Og::groupResolverHandler()->getPlugins(GroupResolverHandlerInterface::RETURN_ONLY_ACTIVE); $this->assertEmpty($plugins); // Enable a plugin. - Og::contextHandler()->updatePlugin('entity', ['status' => 1]); + Og::groupResolverHandler()->updatePlugin('entity', ['status' => 1]); - $plugins = Og::contextHandler()->getPlugins(GroupResolverHandlerInterface::RETURN_ONLY_ACTIVE); + $plugins = Og::groupResolverHandler()->getPlugins(GroupResolverHandlerInterface::RETURN_ONLY_ACTIVE); $this->assertArrayHasKey('entity', $plugins); $this->assertArrayNotHasKey('user_group_reference', $plugins); // Enable the second plugin and change the weight. - Og::contextHandler()->updatePlugin('entity', ['weight' => 1]); - Og::contextHandler()->updatePlugin('user_group_reference', ['weight' => 0, 'status' => 1]); + Og::groupResolverHandler()->updatePlugin('entity', ['weight' => 1]); + Og::groupResolverHandler()->updatePlugin('user_group_reference', ['weight' => 0, 'status' => 1]); - $plugins = Og::contextHandler()->getPlugins(GroupResolverHandlerInterface::RETURN_ONLY_ACTIVE); + $plugins = Og::groupResolverHandler()->getPlugins(GroupResolverHandlerInterface::RETURN_ONLY_ACTIVE); $this->assertArrayHasKey('entity', $plugins); $this->assertArrayHasKey('user_group_reference', $plugins); @@ -86,9 +86,9 @@ public function testOgContextPluginsList() { $this->assertTrue($entity_position > $user_group_reference_position); // Change the weight. - Og::contextHandler()->updatePlugin('entity', ['weight' => -1]); + Og::groupResolverHandler()->updatePlugin('entity', ['weight' => -1]); - $plugins = Og::contextHandler()->getPlugins(GroupResolverHandlerInterface::RETURN_ONLY_ACTIVE); + $plugins = Og::groupResolverHandler()->getPlugins(GroupResolverHandlerInterface::RETURN_ONLY_ACTIVE); list($entity_position, $user_group_reference_position) = $get_plugins_location(array_keys($plugins)); $this->assertTrue($user_group_reference_position > $entity_position); From 5aea9ee3491a01362d3fe166c493a7904010b729 Mon Sep 17 00:00:00 2001 From: Roy Segall Date: Sat, 6 Aug 2016 21:05:22 +0300 Subject: [PATCH 28/32] Fix code sniffer. --- src/GroupResolverHandler.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/GroupResolverHandler.php b/src/GroupResolverHandler.php index 8626d56f8..2967d9dca 100644 --- a/src/GroupResolverHandler.php +++ b/src/GroupResolverHandler.php @@ -9,7 +9,6 @@ use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; -use Drupal\og\Entity\GroupResolverNegotiation; /** * Class GroupResolverHandler. @@ -79,7 +78,7 @@ public function getGroup() { */ public function getPlugins($return_mode = GroupResolverHandlerInterface::RETURN_ONLY_ACTIVE) { - /** @var GroupResolverNegotiation[] $group_resolver_config */ + /** @var \Drupal\og\Entity\GroupResolverNegotiation[] $group_resolver_config */ $group_resolver_config = $this->storage->loadMultiple(); $plugins = $this->pluginManager->getDefinitions(); From 9fce2213ac0dba8b0c645a1124f5fe4a9b169563 Mon Sep 17 00:00:00 2001 From: Roy Segall Date: Sat, 6 Aug 2016 21:42:07 +0300 Subject: [PATCH 29/32] Fix tests. --- tests/src/Kernel/Entity/OgContextTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/src/Kernel/Entity/OgContextTest.php b/tests/src/Kernel/Entity/OgContextTest.php index c8cc20260..e30d12e15 100644 --- a/tests/src/Kernel/Entity/OgContextTest.php +++ b/tests/src/Kernel/Entity/OgContextTest.php @@ -23,7 +23,7 @@ class OgContextTest extends KernelTestBase { /** * {@inheritdoc} */ - public static $modules = ['og', 'system', 'og_test']; + public static $modules = ['og', 'system', 'og_test', 'field']; /** * {@inheritdoc} From f59e52179c89f2e1442190799463eaab3d157928 Mon Sep 17 00:00:00 2001 From: Roy Segall Date: Fri, 26 Aug 2016 16:16:37 +0300 Subject: [PATCH 30/32] Extend from the correct class and fix a service. --- src/GroupResolverHandler.php | 2 +- src/OgContextBase.php | 62 +++++++++++++++++++++++++++++ src/OgContextInterface.php | 3 +- src/Plugin/GroupResolver/Entity.php | 8 ++-- 4 files changed, 69 insertions(+), 6 deletions(-) diff --git a/src/GroupResolverHandler.php b/src/GroupResolverHandler.php index 2967d9dca..cc4bfb0a8 100644 --- a/src/GroupResolverHandler.php +++ b/src/GroupResolverHandler.php @@ -64,7 +64,7 @@ public function getGroup() { foreach ($plugins as $plugin) { if ($group = $this->getPlugin($plugin['id'])->getGroup()) { - $groups = array_merge($groups, $group); + $groups = array_merge($groups, [$group]); } } diff --git a/src/OgContextBase.php b/src/OgContextBase.php index 688a8c46f..fe9b8e4d8 100644 --- a/src/OgContextBase.php +++ b/src/OgContextBase.php @@ -34,4 +34,66 @@ public static function create(ContainerInterface $container, array $configuratio ); } + /** + * Gets runtime context values for the given context IDs. + * + * For context-aware plugins to function correctly, all of the contexts that + * they require must be populated with values. So this method should set a + * value for each context that it adds. For example: + * + * @code + * // Determine a specific node to pass as context to a block. + * $node = ... + * + * // Set that specific node as the value of the 'node' context. + * $context = new Context(new ContextDefinition('entity:node'), $node); + * return ['node' => $context]; + * @endcode + * + * On the other hand, there are cases, on which providers no longer are + * possible to provide context objects, even without the value, so the caller + * should not expect it. + * + * @param string[] $unqualified_context_ids + * The requested context IDs. The context provider must only return contexts + * for those IDs. + * + * @return \Drupal\Core\Plugin\Context\ContextInterface[] + * The determined available contexts, keyed by the unqualified context_id. + * + * @see \Drupal\Core\Plugin\Context\ContextProviderInterface:getAvailableContexts() + */ + public function getRuntimeContexts(array $unqualified_context_ids) { + // TODO: Implement getRuntimeContexts() method. + } + + /** + * Gets all available contexts for the purposes of configuration. + * + * When a context aware plugin is being configured, the configuration UI must + * know which named contexts are potentially available, but does not care + * about the value, since the value can be different for each request, and + * might not be available at all during the configuration UI's request. + * + * For example: + * @code + * // During configuration, there is no specific node to pass as context. + * // However, inform the system that a context named 'node' is + * // available, and provide its definition, so that context aware plugins + * // can be configured to use it. When the plugin, for example a block, + * // needs to evaluate the context, the value of this context will be + * // supplied by getRuntimeContexts(). + * $context = new Context(new ContextDefinition('entity:node')); + * return ['node' => $context]; + * @endcode + * + * @return \Drupal\Core\Plugin\Context\ContextInterface[] + * All available contexts keyed by the unqualified context ID. + * + * @see \Drupal\Core\Plugin\Context\ContextProviderInterface::getRuntimeContext() + */ + public function getAvailableContexts() { + // TODO: Implement getAvailableContexts() method. + } + } diff --git a/src/OgContextInterface.php b/src/OgContextInterface.php index 1c5a84482..ca9761279 100644 --- a/src/OgContextInterface.php +++ b/src/OgContextInterface.php @@ -8,11 +8,12 @@ */ use Drupal\Component\Plugin\PluginInspectionInterface; +use Drupal\Core\Plugin\Context\ContextProviderInterface; /** * Defines an interface for OG context plugins. */ -interface OgContextInterface extends PluginInspectionInterface { +interface OgContextInterface extends PluginInspectionInterface, ContextProviderInterface { /** * Return the current group. If no group will be found return Null. diff --git a/src/Plugin/GroupResolver/Entity.php b/src/Plugin/GroupResolver/Entity.php index 852b37db0..074e24684 100644 --- a/src/Plugin/GroupResolver/Entity.php +++ b/src/Plugin/GroupResolver/Entity.php @@ -9,7 +9,7 @@ use Drupal\Core\Entity\ContentEntityBase; use Drupal\Core\Routing\RouteMatchInterface; -use Drupal\og\GroupManager; +use Drupal\og\GroupTypeManager; use Drupal\og\Og; use Drupal\og\OgContextBase; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -50,10 +50,10 @@ class Entity extends OgContextBase { * The plugin implementation definition. * @param \Drupal\Core\Routing\RouteMatchInterface $route_match * The route match service. - * @param \Drupal\og\GroupManager $group_manager + * @param \Drupal\og\GroupTypeManager $group_manager * The group manager service. */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, RouteMatchInterface $route_match, GroupManager $group_manager) { + public function __construct(array $configuration, $plugin_id, $plugin_definition, RouteMatchInterface $route_match, GroupTypeManager $group_manager) { parent::__construct($configuration, $plugin_id, $plugin_definition); $this->routeMatch = $route_match; $this->groupManger = $group_manager; @@ -68,7 +68,7 @@ public static function create(ContainerInterface $container, array $configuratio $plugin_id, $plugin_definition, $container->get('current_route_match'), - $container->get('og.group.manager') + $container->get('og.group_type_manager') ); } From 23a59c7b0068884a0ec7cf666cfa5c0df2f3a748 Mon Sep 17 00:00:00 2001 From: Roy Segall Date: Fri, 26 Aug 2016 16:21:05 +0300 Subject: [PATCH 31/32] Make CS happy. --- src/OgContextBase.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/OgContextBase.php b/src/OgContextBase.php index fe9b8e4d8..d83417d16 100644 --- a/src/OgContextBase.php +++ b/src/OgContextBase.php @@ -64,7 +64,7 @@ public static function create(ContainerInterface $container, array $configuratio * @see \Drupal\Core\Plugin\Context\ContextProviderInterface:getAvailableContexts() */ public function getRuntimeContexts(array $unqualified_context_ids) { - // TODO: Implement getRuntimeContexts() method. + return []; } /** @@ -93,7 +93,7 @@ public function getRuntimeContexts(array $unqualified_context_ids) { * @see \Drupal\Core\Plugin\Context\ContextProviderInterface::getRuntimeContext() */ public function getAvailableContexts() { - // TODO: Implement getAvailableContexts() method. + return []; } } From e9c7f0adb69643639546496ed9e751d0403916dd Mon Sep 17 00:00:00 2001 From: Roy Segall Date: Sat, 27 Aug 2016 23:50:48 +0300 Subject: [PATCH 32/32] Adding test. --- tests/src/Functional/GroupContextTest.php | 156 ++++++++++++++++++++++ 1 file changed, 156 insertions(+) create mode 100644 tests/src/Functional/GroupContextTest.php diff --git a/tests/src/Functional/GroupContextTest.php b/tests/src/Functional/GroupContextTest.php new file mode 100644 index 000000000..35f8184bf --- /dev/null +++ b/tests/src/Functional/GroupContextTest.php @@ -0,0 +1,156 @@ +groupOwner = $this->drupalCreateUser(); + $this->groupEditor = $this->drupalCreateUser(); + $this->normalUser = $this->drupalCreateUser(); + + $this->setUpContentEntity(); + $this->setUpEntityTestEntity(); + } + + /** + * Setup dummy content group entity and appropriate og permissions. + */ + public function setUpContentEntity() { + // Create a node bundle called 'content_group' and make it an Og group. + $this->createContentType(['type' => 'content_group']); + Og::groupTypeManager()->addGroup('node', 'content_group'); + + // Create a role with only the 'update group' permission. + $content_editor_role = OgRole::create(); + $content_editor_role + ->setName('content_editor') + ->setLabel('Content group editor') + ->setGroupType('node') + ->setGroupBundle('content_group') + ->grantPermission(OgAccess::UPDATE_GROUP_PERMISSION) + ->save(); + + // Create a group content owned by the group owner. + $values = [ + 'title' => $this->randomString(), + 'type' => 'content_group', + 'uid' => $this->groupOwner->id(), + 'status' => 1, + ]; + $this->contentGroup = Node::create($values); + $this->contentGroup->save(); + + // Subscribe the editor user to the groups. + $membership = Og::createMembership($this->contentGroup, $this->groupEditor); + $membership + ->setRoles([$content_editor_role]) + ->save(); + } + + /** + * Setup dummy entity_test group entity and appropriate OG permissions. + */ + public function setUpEntityTestEntity() { + // Create an entity_test bundle called 'entity_group' and make it + // an Og group. + entity_test_create_bundle('entity_group'); + Og::groupTypeManager()->addGroup('entity_test', 'entity_group'); + + // Create a role with only the 'update group' permission. + $entity_editor_role = OgRole::create(); + $entity_editor_role + ->setName('entity_editor') + ->setLabel('Entity group editor') + ->setGroupType('entity_test') + ->setGroupBundle('entity_group') + ->grantPermission(OgAccess::UPDATE_GROUP_PERMISSION) + ->save(); + + // Create an entity_test entity owned by the group owner. + $values = [ + 'title' => $this->randomString(), + 'type' => 'entity_group', + 'uid' => $this->groupOwner->id(), + ]; + $this->entityGroup = EntityTest::create($values); + $this->entityGroup->save(); + + $membership = Og::createMembership($this->entityGroup, $this->groupEditor); + $membership + ->setRoles([$entity_editor_role]) + ->save(); + + Og::groupResolverHandler()->updatePlugin('entity', ['status' => TRUE]); + } + + function testGroupContext() { + $this->drupalLogin($this->groupEditor); + $this->drupalGet($this->contentGroup->toUrl('edit-form')); + $this->assertNotEmpty(Og::groupResolverHandler()->getGroup()); + } + +}