View source
<?php
namespace Drupal\groupmenu\Plugin\GroupContentEnabler;
use Drupal\Core\Session\AccountInterface;
use Drupal\group\Access\GroupAccessResult;
use Drupal\group\Entity\GroupContentInterface;
use Drupal\group\Entity\GroupInterface;
use Drupal\group\Plugin\GroupContentEnablerBase;
use Drupal\Core\Url;
use Drupal\Core\Form\FormStateInterface;
class GroupMenu extends GroupContentEnablerBase {
public function getGroupOperations(GroupInterface $group) {
$account = \Drupal::currentUser();
$plugin_id = $this
->getPluginId();
$operations = [];
if ($group
->hasPermission("create {$plugin_id} entity", $account)) {
$route_params = [
'group' => $group
->id(),
'plugin_id' => $plugin_id,
];
$operations["groupmenu-create"] = [
'title' => $this
->t('Create menu'),
'url' => new Url('entity.group_content.create_form', $route_params),
'weight' => 30,
];
}
return $operations;
}
public function getPermissions() {
$plugin_id = $this
->getPluginId();
$t_args = [
'%plugin_name' => $this
->getLabel(),
'%entity_type' => $this
->getEntityType()
->getSingularLabel(),
];
$defaults = [
'title_args' => $t_args,
'description_args' => $t_args,
];
$entity_prefix = '%plugin_name - Entity:';
$relation_prefix = '%plugin_name - Relationship:';
$permissions["view {$plugin_id} entity"] = [
'title' => "{$entity_prefix} View %entity_type entities",
] + $defaults;
$permissions["create {$plugin_id} entity"] = [
'title' => "{$entity_prefix} Add %entity_type entities",
'description' => 'Allows you to create a new %entity_type entity and relate it to the group.',
] + $defaults;
$permissions["update {$plugin_id} entity"] = [
'title' => "{$entity_prefix} Edit %entity_type entities",
] + $defaults;
$permissions["delete {$plugin_id} entity"] = [
'title' => "{$entity_prefix} Delete %entity_type entities",
] + $defaults;
$permissions["view {$plugin_id} content"] = [
'title' => "{$relation_prefix} View entity relations",
] + $defaults;
$permissions["create {$plugin_id} content"] = [
'title' => "{$relation_prefix} Add entity relation",
'description' => 'Allows you to relate an existing %entity_type entity to the group.',
] + $defaults;
$permissions["update {$plugin_id} content"] = [
'title' => "{$relation_prefix} Edit entity relations",
] + $defaults;
$permissions["delete {$plugin_id} content"] = [
'title' => "{$relation_prefix} Delete entity relations",
] + $defaults;
return $permissions;
}
protected function updateAccess(GroupContentInterface $group_content, AccountInterface $account) {
$group = $group_content
->getGroup();
$plugin_id = $this
->getPluginId();
return GroupAccessResult::allowedIfHasGroupPermission($group, $account, "update {$plugin_id} content");
}
protected function deleteAccess(GroupContentInterface $group_content, AccountInterface $account) {
$group = $group_content
->getGroup();
$plugin_id = $this
->getPluginId();
return GroupAccessResult::allowedIfHasGroupPermission($group, $account, "delete {$plugin_id} content");
}
public function defaultConfiguration() {
$config = parent::defaultConfiguration();
$config['entity_cardinality'] = 1;
$config['node_form_group_menu'] = 1;
$config['auto_create_group_menu'] = FALSE;
$config['auto_create_home_link'] = FALSE;
$config['auto_create_home_link_title'] = 'Home';
return $config;
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$configuration = $this
->getConfiguration();
$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>';
$form['auto_create_group_menu'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Automatically create a menu when a group is created.'),
'#description' => $this
->t('The menu will be added to the new group as a group menu. The menu will be deleted when group is deleted.'),
'#default_value' => $configuration['auto_create_group_menu'],
];
$form['auto_create_home_link'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Automatically create a "Home" link for the menu.'),
'#description' => $this
->t('The "Home" link will link to the canonical URL of the group.'),
'#default_value' => $configuration['auto_create_home_link'],
'#states' => [
'visible' => [
':input[name="auto_create_group_menu"]' => [
'checked' => TRUE,
],
],
],
];
$form['auto_create_home_link_title'] = [
'#type' => 'textfield',
'#title' => $this
->t('Link title'),
'#default_value' => $configuration['auto_create_home_link_title'],
'#required' => TRUE,
'#states' => [
'visible' => [
':input[name="auto_create_home_link"]' => [
'checked' => TRUE,
],
],
],
];
return $form;
}
}