From 65665384c81d8ad6435a661e15420c38b27497d4 Mon Sep 17 00:00:00 2001 From: Craig McEldowney Date: Fri, 14 Apr 2017 19:35:57 -0700 Subject: [PATCH] Add example for how to expose custom entity to Group module --- advertiser.module | 12 +++ .../GroupContentEnabler/AdvertiserEntity.php | 99 +++++++++++++++++++ .../AdvertiserEntityDeriver.php | 35 +++++++ 3 files changed, 146 insertions(+) create mode 100644 advertiser.module create mode 100644 src/Plugin/GroupContentEnabler/AdvertiserEntity.php create mode 100644 src/Plugin/GroupContentEnabler/AdvertiserEntityDeriver.php diff --git a/advertiser.module b/advertiser.module new file mode 100644 index 0000000..1adef45 --- /dev/null +++ b/advertiser.module @@ -0,0 +1,12 @@ +addContent($ad, 'advertiser:advertiser'); +} diff --git a/src/Plugin/GroupContentEnabler/AdvertiserEntity.php b/src/Plugin/GroupContentEnabler/AdvertiserEntity.php new file mode 100644 index 0000000..9e54e02 --- /dev/null +++ b/src/Plugin/GroupContentEnabler/AdvertiserEntity.php @@ -0,0 +1,99 @@ +getPluginId(); + $type = $this->getEntityBundle(); + $operations = []; + + if ($group->hasPermission("create $plugin_id entity", $account)) { + $route_params = ['group' => $group->id(), 'plugin_id' => $plugin_id]; + $operations["advertiser-create-$type"] = [ + 'title' => $this->t('Create @type', ['@type' => 'advertiser']), + 'url' => new Url('entity.group_content.create_form', $route_params), + 'weight' => 30, + ]; + } + + return $operations; + } + + /** + * {@inheritdoc} + */ + protected function getTargetEntityPermissions() { + $permissions = parent::getTargetEntityPermissions(); + $plugin_id = $this->getPluginId(); + + // Add a 'view unpublished' permission by re-using most of the 'view' one. + $original = $permissions["view $plugin_id entity"]; + $permissions["view unpublished $plugin_id entity"] = [ + 'title' => str_replace('View ', 'View unpublished ', $original['title']), + ] + $original; + + return $permissions; + } + + /** + * {@inheritdoc} + */ + public function defaultConfiguration() { + $config = parent::defaultConfiguration(); + $config['entity_cardinality'] = 1; + return $config; + } + + /** + * {@inheritdoc} + */ + public function buildConfigurationForm(array $form, FormStateInterface $form_state) { + $form = parent::buildConfigurationForm($form, $form_state); + + // Disable the entity cardinality field as the functionality of this module + // relies on a cardinality of 1. We don't just hide it, though, to keep a UI + // that's consistent with other content enabler plugins. + $info = $this->t("This field has been disabled by the plugin to guarantee the functionality that's expected of it."); + $form['entity_cardinality']['#disabled'] = TRUE; + $form['entity_cardinality']['#description'] .= '
' . $info . ''; + + return $form; + } + + /** + * {@inheritdoc} + */ + public function calculateDependencies() { + $dependencies = parent::calculateDependencies(); + $dependencies['config'][] = 'node.type.' . $this->getEntityBundle(); + return $dependencies; + } + +} diff --git a/src/Plugin/GroupContentEnabler/AdvertiserEntityDeriver.php b/src/Plugin/GroupContentEnabler/AdvertiserEntityDeriver.php new file mode 100644 index 0000000..f289129 --- /dev/null +++ b/src/Plugin/GroupContentEnabler/AdvertiserEntityDeriver.php @@ -0,0 +1,35 @@ + $node_type) { +// $label = $node_type->label(); +// $this->derivatives[$name] = [ +// 'entity_bundle' => $name, +// 'label' => t('Group node') . " ($label)", +// 'description' => t('Adds %type content to groups both publicly and privately.', ['%type' => $label]), +// ] + $base_plugin_definition; +// } +// +// return $this->derivatives; + + $this->derivatives['advertiser'] = [ + 'entity_bundle' => 'advertiser', + 'label' => t('Group content: Advertiser'), + 'description' => t('Adds advertiser content to groups both publicly and privately.'), + ] + $base_plugin_definition; + + return $this->derivatives; + + } + +}