Subgroup.php in Subgroup (Graph) 1.0.x
File
src/Plugin/GroupContentEnabler/Subgroup.php
View source
<?php
namespace Drupal\ggroup\Plugin\GroupContentEnabler;
use Drupal\group\Entity\GroupInterface;
use Drupal\group\Plugin\GroupContentEnablerBase;
use Drupal\group\Entity\GroupType;
use Drupal\Core\Url;
use Drupal\Core\Form\FormStateInterface;
class Subgroup extends GroupContentEnablerBase {
protected function getSubgroupType() {
return GroupType::load($this
->getEntityBundle());
}
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(),
'group_type' => $this
->getEntityBundle(),
];
$operations["ggroup_create-{$type}"] = [
'title' => $this
->t('Create @type', [
'@type' => $this
->getSubgroupType()
->label(),
]),
'url' => new Url('entity.group_content.subgroup_add_form', $route_params),
'weight' => 35,
];
}
return $operations;
}
public function defaultConfiguration() {
$config = parent::defaultConfiguration();
$config['entity_cardinality'] = 1;
if ($this
->getGroupType()) {
$parent_roles = $this
->getGroupType()
->getRoles();
foreach ($parent_roles as $role_id => $role) {
$config['parent_role_mapping'][$role_id] = NULL;
}
}
if ($this
->getSubgroupType()) {
$child_roles = $this
->getSubgroupType()
->getRoles();
foreach ($child_roles as $role_id => $role) {
$config['child_role_mapping'][$role_id] = NULL;
}
}
return $config;
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$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;
}
public function calculateDependencies() {
return [
'config' => [
"group.type.{$this->getEntityBundle()}",
],
];
}
}
Classes
Name |
Description |
Subgroup |
Provides a content enabler for subgroups. |