class SocialGroupSelectorWidget in Open Social 10.1.x
Same name and namespace in other branches
- 8.9 modules/social_features/social_group/src/Plugin/Field/FieldWidget/SocialGroupSelectorWidget.php \Drupal\social_group\Plugin\Field\FieldWidget\SocialGroupSelectorWidget
- 8 modules/social_features/social_group/src/Plugin/Field/FieldWidget/SocialGroupSelectorWidget.php \Drupal\social_group\Plugin\Field\FieldWidget\SocialGroupSelectorWidget
- 8.2 modules/social_features/social_group/src/Plugin/Field/FieldWidget/SocialGroupSelectorWidget.php \Drupal\social_group\Plugin\Field\FieldWidget\SocialGroupSelectorWidget
- 8.3 modules/social_features/social_group/src/Plugin/Field/FieldWidget/SocialGroupSelectorWidget.php \Drupal\social_group\Plugin\Field\FieldWidget\SocialGroupSelectorWidget
- 8.4 modules/social_features/social_group/src/Plugin/Field/FieldWidget/SocialGroupSelectorWidget.php \Drupal\social_group\Plugin\Field\FieldWidget\SocialGroupSelectorWidget
- 8.5 modules/social_features/social_group/src/Plugin/Field/FieldWidget/SocialGroupSelectorWidget.php \Drupal\social_group\Plugin\Field\FieldWidget\SocialGroupSelectorWidget
- 8.6 modules/social_features/social_group/src/Plugin/Field/FieldWidget/SocialGroupSelectorWidget.php \Drupal\social_group\Plugin\Field\FieldWidget\SocialGroupSelectorWidget
- 8.7 modules/social_features/social_group/src/Plugin/Field/FieldWidget/SocialGroupSelectorWidget.php \Drupal\social_group\Plugin\Field\FieldWidget\SocialGroupSelectorWidget
- 8.8 modules/social_features/social_group/src/Plugin/Field/FieldWidget/SocialGroupSelectorWidget.php \Drupal\social_group\Plugin\Field\FieldWidget\SocialGroupSelectorWidget
- 10.3.x modules/social_features/social_group/src/Plugin/Field/FieldWidget/SocialGroupSelectorWidget.php \Drupal\social_group\Plugin\Field\FieldWidget\SocialGroupSelectorWidget
- 10.0.x modules/social_features/social_group/src/Plugin/Field/FieldWidget/SocialGroupSelectorWidget.php \Drupal\social_group\Plugin\Field\FieldWidget\SocialGroupSelectorWidget
- 10.2.x modules/social_features/social_group/src/Plugin/Field/FieldWidget/SocialGroupSelectorWidget.php \Drupal\social_group\Plugin\Field\FieldWidget\SocialGroupSelectorWidget
A widget to select a group when creating an entity in a group.
Plugin annotation
@FieldWidget(
id = "social_group_selector_widget",
label = @Translation("Social group select list"),
field_types = {
"entity_reference",
"list_integer",
"list_float",
"list_string"
},
multiple_values = TRUE
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\Core\Field\PluginSettingsBase implements DependentPluginInterface, PluginSettingsInterface
- class \Drupal\Core\Field\WidgetBase implements WidgetInterface, ContainerFactoryPluginInterface
- class \Drupal\Core\Field\Plugin\Field\FieldWidget\OptionsWidgetBase
- class \Drupal\Core\Field\Plugin\Field\FieldWidget\OptionsSelectWidget
- class \Drupal\social_group\Plugin\Field\FieldWidget\SocialGroupSelectorWidget implements ContainerFactoryPluginInterface
- class \Drupal\Core\Field\Plugin\Field\FieldWidget\OptionsSelectWidget
- class \Drupal\Core\Field\Plugin\Field\FieldWidget\OptionsWidgetBase
- class \Drupal\Core\Field\WidgetBase implements WidgetInterface, ContainerFactoryPluginInterface
- class \Drupal\Core\Field\PluginSettingsBase implements DependentPluginInterface, PluginSettingsInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of SocialGroupSelectorWidget
File
- modules/
social_features/ social_group/ src/ Plugin/ Field/ FieldWidget/ SocialGroupSelectorWidget.php, line 39
Namespace
Drupal\social_group\Plugin\Field\FieldWidgetView source
class SocialGroupSelectorWidget extends OptionsSelectWidget implements ContainerFactoryPluginInterface {
protected $configFactory;
protected $moduleHander;
protected $currentUser;
protected $pluginManager;
protected $entityTypeManager;
protected $userManager;
/**
* Creates a SocialGroupSelectorWidget instance.
*
* {@inheritdoc}
*/
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, array $third_party_settings, ConfigFactoryInterface $configFactory, AccountProxyInterface $currentUser, ModuleHandler $moduleHandler, GroupContentEnablerManager $pluginManager, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $third_party_settings);
$this->configFactory = $configFactory;
$this->moduleHander = $moduleHandler;
$this->currentUser = $currentUser;
$this->pluginManager = $pluginManager;
$this->entityTypeManager = $entity_type_manager;
$this->userManager = $entity_type_manager
->getStorage('user');
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($plugin_id, $plugin_definition, $configuration['field_definition'], $configuration['settings'], $configuration['third_party_settings'], $container
->get('config.factory'), $container
->get('current_user'), $container
->get('module_handler'), $container
->get('plugin.manager.group_content_enabler'), $container
->get('entity_type.manager'));
}
/**
* Returns the array of options for the widget.
*
* @param \Drupal\Core\Entity\FieldableEntityInterface $entity
* The entity for which to return options.
*
* @return array
* The array of options for the widget.
*/
protected function getOptions(FieldableEntityInterface $entity) {
// Must be a node.
if ($entity
->getEntityTypeId() !== 'node') {
// We only handle nodes. When using this widget on other content types,
// we simply return the normal options.
return parent::getOptions($entity);
}
// Get the bundle fron the node.
$entity_type = $entity
->bundle();
/** @var \Drupal\user\Entity\User $account */
$account = $this->userManager
->load($this->currentUser
->id());
// If the user can administer content and groups, we allow them to
// override this. Otherwise we stick to the original owner.
if (!$account
->hasPermission('administer nodes') && !$account
->hasPermission('manage all groups')) {
$account = $entity
->getOwner();
}
// Limit the settable options for the current user account.
$options = $this->fieldDefinition
->getFieldStorageDefinition()
->getOptionsProvider($this->column, $entity)
->getSettableOptions($account);
// Check for each group type if the content type is installed.
foreach ($options as $key => $optgroup) {
// Groups are in the array below.
if (is_array($optgroup)) {
// Loop through the groups.
foreach ($optgroup as $gid => $title) {
// If the group exists.
if ($group = Group::load($gid)) {
// Load all installed plugins for this group type.
$plugin_ids = $this->pluginManager
->getInstalledIds($group
->getGroupType());
// If the bundle is not installed,
// then unset the entire optiongroup (=group type).
if (!in_array('group_node:' . $entity_type, $plugin_ids)) {
unset($options[$key]);
}
}
// We need to check only one of each group type,
// so break out the second each.
break;
}
}
}
// Remove groups the user does not have create access to.
if (!$account
->hasPermission('manage all groups')) {
$options = $this
->removeGroupsWithoutCreateAccess($options, $account, $entity);
}
// Add an empty option if the widget needs one.
if ($empty_label = $this
->getEmptyLabel()) {
$options = [
'_none' => $empty_label,
] + $options;
}
$module_handler = $this->moduleHander;
$context = [
'fieldDefinition' => $this->fieldDefinition,
'entity' => $entity,
];
$module_handler
->alter('options_list', $options, $context);
array_walk_recursive($options, [
$this,
'sanitizeLabel',
]);
return $options;
}
/**
* {@inheritdoc}
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$element = parent::formElement($items, $delta, $element, $form, $form_state);
$element['#suffix'] = '<div id="group-selection-result"></div>';
$element['#ajax'] = [
'callback' => __CLASS__ . '::validateGroupSelection',
'effect' => 'fade',
'event' => 'change',
];
// Unfortunately validateGroupSelection is cast as a static function
// So I have to add this setting to the form in order to use it later on.
$default_visibility = $this->configFactory
->get('entity_access_by_field.settings')
->get('default_visibility');
$form['default_visibility'] = [
'#type' => 'value',
'#value' => $default_visibility,
];
$change_group_node = $this->configFactory
->get('social_group.settings')
->get('allow_group_selection_in_node');
/** @var \Drupal\Core\Entity\EntityInterface $entity */
$entity = $form_state
->getFormObject()
->getEntity();
// If it is a new node lets add the current group.
if (!$entity
->id()) {
$current_group = _social_group_get_current_group();
if (!empty($current_group) && empty($element['#default_value'])) {
$element['#default_value'] = [
$current_group
->id(),
];
}
}
else {
if (!$change_group_node && !$this->currentUser
->hasPermission('manage all groups')) {
$element['#disabled'] = TRUE;
$element['#description'] = t('Moving content after creation function has been disabled. In order to move this content, please contact a site manager.');
}
}
return $element;
}
/**
* Validate the group selection and change the visibility settings.
*
* @param array $form
* Form to process.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* Form state to process.
*
* @return \Drupal\Core\Ajax\AjaxResponse
* Response changing values of the visibility field and set status message.
*/
public static function validateGroupSelection(array $form, FormStateInterface $form_state) {
$ajax_response = new AjaxResponse();
$entity = $form_state
->getFormObject()
->getEntity();
$selected_visibility = $form_state
->getValue('field_content_visibility');
if (!empty($selected_visibility)) {
$selected_visibility = $selected_visibility['0']['value'];
}
if ($selected_groups = $form_state
->getValue('groups')) {
foreach ($selected_groups as $selected_group_key => $selected_group) {
$gid = $selected_group['target_id'];
$group = Group::load($gid);
$group_type_id = $group
->getGroupType()
->id();
$allowed_visibility_options = social_group_get_allowed_visibility_options_per_group_type($group_type_id, NULL, $entity, $group);
// @todo Add support for multiple groups, for now just process 1 group.
break;
}
}
else {
$default_visibility = $form_state
->getValue('default_visibility');
$allowed_visibility_options = social_group_get_allowed_visibility_options_per_group_type(NULL, NULL, $entity);
$ajax_response
->addCommand(new InvokeCommand('#edit-field-content-visibility-' . $default_visibility, 'prop', [
'checked',
'checked',
]));
}
foreach ($allowed_visibility_options as $visibility => $allowed) {
$ajax_response
->addCommand(new InvokeCommand('#edit-field-content-visibility-' . $visibility, 'addClass', [
'js--animate-enabled-form-control',
]));
if ($allowed === TRUE) {
$ajax_response
->addCommand(new InvokeCommand('#edit-field-content-visibility-' . $visibility, 'removeAttr', [
'disabled',
]));
if (empty($default_visibility) || $visibility === $default_visibility) {
$ajax_response
->addCommand(new InvokeCommand('#edit-field-content-visibility-' . $visibility, 'prop', [
'checked',
'checked',
]));
}
}
else {
if ($selected_visibility && $selected_visibility === $visibility) {
$ajax_response
->addCommand(new InvokeCommand('#edit-field-content-visibility-' . $visibility, 'removeAttr', [
'checked',
]));
}
$ajax_response
->addCommand(new InvokeCommand('#edit-field-content-visibility-' . $visibility, 'prop', [
'disabled',
'disabled',
]));
}
$ajax_response
->addCommand(new InvokeCommand('#edit-field-content-visibility-' . $visibility, 'change'));
}
$text = t('Changing the group may have impact on the <strong>visibility settings</strong> and may cause <strong>author/co-authors</strong> to lose access.');
\Drupal::messenger()
->addStatus($text);
$ajax_response
->addCommand(new HtmlCommand('#group-selection-result', $text));
return $ajax_response;
}
/**
* Remove options from the list.
*
* @param array $options
* A list of options to check.
* @param \Drupal\user\Entity\User $account
* The user to check for.
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity to check for.
*
* @return array
* An list of options for the field containing groups with create access.
*/
private function removeGroupsWithoutCreateAccess(array $options, User $account, EntityInterface $entity) {
foreach ($options as $option_category_key => $groups_in_category) {
if (is_array($groups_in_category)) {
foreach ($groups_in_category as $gid => $group_title) {
if (!$this
->checkGroupContentCreateAccess($gid, $account, $entity)) {
unset($options[$option_category_key][$gid]);
}
}
// Remove the entire category if there are no groups for this author.
if (empty($options[$option_category_key])) {
unset($options[$option_category_key]);
}
}
else {
if (!$this
->checkGroupContentCreateAccess($option_category_key, $account, $entity)) {
unset($options[$option_category_key]);
}
}
}
return $options;
}
/**
* Check if user may create content of bundle in group.
*
* @param int $gid
* Group id.
* @param \Drupal\user\Entity\User $account
* The user to check for.
* @param \Drupal\Core\Entity\EntityInterface $entity
* The node bundle to check for.
*
* @return int
* Either TRUE or FALSE.
*/
private function checkGroupContentCreateAccess($gid, User $account, EntityInterface $entity) {
$group = Group::load($gid);
if ($group
->hasPermission('create group_' . $entity
->getEntityTypeId() . ':' . $entity
->bundle() . ' entity', $account)) {
if ($group
->getGroupType()
->id() === 'public_group') {
$config = $this->configFactory
->get('entity_access_by_field.settings');
if ($config
->get('disable_public_visibility') === 1 && !$account
->hasPermission('override disabled public visibility')) {
return FALSE;
}
}
return TRUE;
}
return FALSE;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
public | function | 2 | |
DependencySerializationTrait:: |
public | function | 2 | |
MessengerTrait:: |
protected | property | The messenger. | 27 |
MessengerTrait:: |
public | function | Gets the messenger. | 27 |
MessengerTrait:: |
public | function | Sets the messenger. | |
OptionsSelectWidget:: |
protected | function |
Returns the empty option label to add to the list of options, if any. Overrides OptionsWidgetBase:: |
|
OptionsSelectWidget:: |
protected | function |
Sanitizes a string label to display as an option. Overrides OptionsWidgetBase:: |
|
OptionsSelectWidget:: |
protected | function |
Indicates whether the widgets support optgroups. Overrides OptionsWidgetBase:: |
|
OptionsWidgetBase:: |
protected | property | Abstract over the actual field columns, to allow different field types to reuse those widgets. | |
OptionsWidgetBase:: |
protected | function | Determines selected options from the incoming field values. | |
OptionsWidgetBase:: |
public static | function | Form validation handler for widget elements. | 1 |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
2 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginSettingsBase:: |
protected | property | Whether default settings have been merged into the current $settings. | |
PluginSettingsBase:: |
protected | property | The plugin settings injected by third party modules. | |
PluginSettingsBase:: |
public | function |
Calculates dependencies for the configured plugin. Overrides DependentPluginInterface:: |
6 |
PluginSettingsBase:: |
public static | function |
Defines the default settings for this plugin. Overrides PluginSettingsInterface:: |
42 |
PluginSettingsBase:: |
public | function |
Returns the value of a setting, or its default value if absent. Overrides PluginSettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Returns the array of settings, including defaults for missing settings. Overrides PluginSettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Gets the list of third parties that store information. Overrides ThirdPartySettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Gets the value of a third-party setting. Overrides ThirdPartySettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Gets all third-party settings of a given module. Overrides ThirdPartySettingsInterface:: |
|
PluginSettingsBase:: |
protected | function | Merges default settings values into $settings. | |
PluginSettingsBase:: |
public | function |
Informs the plugin that some configuration it depends on will be deleted. Overrides PluginSettingsInterface:: |
3 |
PluginSettingsBase:: |
public | function |
Sets the value of a setting for the plugin. Overrides PluginSettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Sets the settings for the plugin. Overrides PluginSettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Sets the value of a third-party setting. Overrides ThirdPartySettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Unsets a third-party setting. Overrides ThirdPartySettingsInterface:: |
|
SocialGroupSelectorWidget:: |
protected | property | ||
SocialGroupSelectorWidget:: |
protected | property | ||
SocialGroupSelectorWidget:: |
protected | property | ||
SocialGroupSelectorWidget:: |
protected | property | ||
SocialGroupSelectorWidget:: |
protected | property | ||
SocialGroupSelectorWidget:: |
protected | property | ||
SocialGroupSelectorWidget:: |
private | function | Check if user may create content of bundle in group. | |
SocialGroupSelectorWidget:: |
public static | function |
Creates an instance of the plugin. Overrides WidgetBase:: |
|
SocialGroupSelectorWidget:: |
public | function |
Returns the form for a single field widget. Overrides OptionsSelectWidget:: |
|
SocialGroupSelectorWidget:: |
protected | function |
Returns the array of options for the widget. Overrides OptionsWidgetBase:: |
|
SocialGroupSelectorWidget:: |
private | function | Remove options from the list. | |
SocialGroupSelectorWidget:: |
public static | function | Validate the group selection and change the visibility settings. | |
SocialGroupSelectorWidget:: |
public | function |
Creates a SocialGroupSelectorWidget instance. Overrides OptionsWidgetBase:: |
|
StringTranslationTrait:: |
protected | property | The string translation service. | 4 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
WidgetBase:: |
protected | property | The field definition. | |
WidgetBase:: |
protected | property |
The widget settings. Overrides PluginSettingsBase:: |
|
WidgetBase:: |
public static | function | Ajax callback for the "Add another item" button. | |
WidgetBase:: |
public static | function | Submission handler for the "Add another item" button. | |
WidgetBase:: |
public static | function | After-build handler for field elements in a form. | |
WidgetBase:: |
public | function |
Assigns a field-level validation error to the right widget sub-element. Overrides WidgetInterface:: |
8 |
WidgetBase:: |
public | function |
Extracts field values from submitted form values. Overrides WidgetBaseInterface:: |
2 |
WidgetBase:: |
public | function |
Reports field-level validation errors against actual form elements. Overrides WidgetBaseInterface:: |
2 |
WidgetBase:: |
public | function |
Creates a form element for a field. Overrides WidgetBaseInterface:: |
3 |
WidgetBase:: |
protected | function | Special handling to create form elements for multiple values. | 1 |
WidgetBase:: |
protected | function | Generates the form element for a single copy of the widget. | |
WidgetBase:: |
protected | function | Returns the value of a field setting. | |
WidgetBase:: |
protected | function | Returns the array of field settings. | |
WidgetBase:: |
protected | function | Returns the filtered field description. | |
WidgetBase:: |
public static | function |
Retrieves processing information about the widget from $form_state. Overrides WidgetBaseInterface:: |
|
WidgetBase:: |
protected static | function | Returns the location of processing information within $form_state. | |
WidgetBase:: |
protected | function | Returns whether the widget handles multiple values. | |
WidgetBase:: |
public static | function |
Returns if the widget can be used for the provided field. Overrides WidgetInterface:: |
4 |
WidgetBase:: |
protected | function | Returns whether the widget used for default value form. | |
WidgetBase:: |
public | function |
Massages the form values into the format expected for field values. Overrides WidgetInterface:: |
7 |
WidgetBase:: |
public | function |
Returns a form to configure settings for the widget. Overrides WidgetInterface:: |
16 |
WidgetBase:: |
public | function |
Returns a short summary for the current widget settings. Overrides WidgetInterface:: |
15 |
WidgetBase:: |
public static | function |
Stores processing information about the widget in $form_state. Overrides WidgetBaseInterface:: |