Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions advertiser.module
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

use Drupal\group\Entity\Group;


function _advertiser_show_adding_to_group() {
// Make sure you have added an Advertiser and a Group
$ad = \Drupal\advertiser\Entity\Advertiser::load(1);
$group = Group::load(1);

$group->addContent($ad, 'advertiser:advertiser');
}
99 changes: 99 additions & 0 deletions src/Plugin/GroupContentEnabler/AdvertiserEntity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

namespace Drupal\advertiser\Plugin\GroupContentEnabler;

use Drupal\group\Entity\GroupInterface;
use Drupal\group\Plugin\GroupContentEnablerBase;
use Drupal\node\Entity\NodeType;
use Drupal\Core\Url;
use Drupal\Core\Form\FormStateInterface;

/**
* Provides a content enabler for nodes.
*
* @GroupContentEnabler(
* id = "advertiser",
* label = @Translation("Advertiser"),
* description = @Translation("Adds advertisers to groups."),
* entity_type_id = "advertiser",
* entity_access = TRUE,
* pretty_path_key = "node",
* reference_label = @Translation("Title"),
* reference_description = @Translation("The name of the advertiser to add to the group"),
* deriver = "Drupal\advertiser\Plugin\GroupContentEnabler\AdvertiserEntityDeriver"
* )
*/
class AdvertiserEntity extends GroupContentEnablerBase {

/**
* {@inheritdoc}
*/
public function getGroupOperations(GroupInterface $group) {
$account = \Drupal::currentUser();
$plugin_id = $this->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'] .= '<br /><em>' . $info . '</em>';

return $form;
}

/**
* {@inheritdoc}
*/
public function calculateDependencies() {
$dependencies = parent::calculateDependencies();
$dependencies['config'][] = 'node.type.' . $this->getEntityBundle();
return $dependencies;
}

}
35 changes: 35 additions & 0 deletions src/Plugin/GroupContentEnabler/AdvertiserEntityDeriver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Drupal\advertiser\Plugin\GroupContentEnabler;

use Drupal\node\Entity\NodeType;
use Drupal\Component\Plugin\Derivative\DeriverBase;

class AdvertiserEntityDeriver extends DeriverBase {

/**
* {@inheritdoc}.
*/
public function getDerivativeDefinitions($base_plugin_definition) {
// foreach (NodeType::loadMultiple() as $name => $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;

}

}