class UserFlagType in Flag 8.4
Provides a flag type for user entities.
Plugin annotation
@FlagType(
id = "entity:user",
title = @Translation("User"),
entity_type = "user",
provider = "user"
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\flag\FlagType\FlagTypeBase implements FlagTypePluginInterface uses StringTranslationTrait
- class \Drupal\flag\Plugin\Flag\EntityFlagType uses StringTranslationTrait
- class \Drupal\flag\Plugin\Flag\UserFlagType
- class \Drupal\flag\Plugin\Flag\EntityFlagType uses StringTranslationTrait
- class \Drupal\flag\FlagType\FlagTypeBase implements FlagTypePluginInterface uses StringTranslationTrait
Expanded class hierarchy of UserFlagType
File
- src/
Plugin/ Flag/ UserFlagType.php, line 21
Namespace
Drupal\flag\Plugin\FlagView source
class UserFlagType extends EntityFlagType {
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
$options = parent::defaultConfiguration();
$options += [
'show_on_profile' => TRUE,
];
return $options;
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
/* Options form extras for user flags */
$form['access']['bundles'] = [
// A user flag doesn't support node types.
// TODO: Maybe support roles instead of node types.
'#type' => 'value',
'#value' => [
0 => 0,
],
];
$form['display']['show_on_profile'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Display link on user profile page'),
'#description' => $this
->t('Show the link formatted as a user profile element.'),
'#default_value' => $this
->showOnProfile(),
// Put this above 'show on entity'.
'#weight' => -1,
];
return $form;
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::submitConfigurationForm($form, $form_state);
$this->configuration['show_on_profile'] = $form_state
->getValue([
'show_on_profile',
]);
}
/**
* {@inheritdoc}
*/
protected function getExtraPermissionsOptions() {
$options = parent::getExtraPermissionsOptions();
// Tweak the UI label from the parent class.
$options['owner'] = $this
->t('Permissions for users to flag themselves.');
return $options;
}
/**
* Specifies if the flag link should appear on the user profile.
*
* @return bool
* TRUE if the flag link appears on the user profile, FALSE otherwise.
*/
public function showOnProfile() {
return $this->configuration['show_on_profile'];
}
/**
* {@inheritdoc}
*/
protected function getExtraPermissionsOwner(FlagInterface $flag) {
$permissions['flag ' . $flag
->id() . ' own user account'] = [
'title' => $this
->t('Flag %flag_title own profile', [
'%flag_title' => $flag
->label(),
]),
];
$permissions['unflag ' . $flag
->id() . ' own user account'] = [
'title' => $this
->t('Unflag %flag_title own profile', [
'%flag_title' => $flag
->label(),
]),
];
$permissions['flag ' . $flag
->id() . ' other user accounts'] = [
'title' => $this
->t("Flag %flag_title others' profiles", [
'%flag_title' => $flag
->label(),
]),
];
$permissions['unflag ' . $flag
->id() . ' other user accounts'] = [
'title' => $this
->t("Unflag %flag_title others' profiles", [
'%flag_title' => $flag
->label(),
]),
];
return $permissions;
}
/**
* {@inheritdoc}
*/
protected function isFlaggableOwnable() {
// The User entity doesn't implement EntityOwnerInterface, but technically
// a user 'owns' themselves. Moreover, the 'owner' permissions are about
// whether the uid property of the flaggable matches the current user, which
// applies to User flaggables too.
return TRUE;
}
/**
* {@inheritdoc}
*/
public function isAddEditForm($operation) {
// The user profile form uses 'default' as the operation for editing, and
// 'register' for adding.
return in_array($operation, [
'register',
'default',
]);
}
/**
* {@inheritdoc}
*/
public function actionAccess($action, FlagInterface $flag, AccountInterface $account, EntityInterface $flaggable = NULL) {
$access = parent::actionAccess($action, $flag, $account, $flaggable);
if ($flaggable && $this
->hasExtraPermission('owner')) {
// Permit selfies.
$permission = $action . ' ' . $flag
->id() . ' own user account';
$selfies_permission_access = AccessResult::allowedIfHasPermission($account, $permission)
->addCacheContexts([
'user',
]);
$account_match_access = AccessResult::allowedIf($account
->id() == $flaggable
->id());
$own_access = $selfies_permission_access
->andIf($account_match_access);
$access = $access
->orIf($own_access);
// Act on others' profiles.
$permission = $action . ' ' . $flag
->id() . ' other user accounts';
$others_permission_access = AccessResult::allowedIfHasPermission($account, $permission)
->addCacheContexts([
'user',
]);
$account_mismatch_access = AccessResult::allowedIf($account
->id() != $flaggable
->id());
$others_access = $others_permission_access
->andIf($account_mismatch_access);
$access = $access
->orIf($others_access);
}
return $access;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
EntityFlagType:: |
protected | property | The entity type defined in plugin definition. | |
EntityFlagType:: |
protected | property | The entity type manager. | |
EntityFlagType:: |
public | function |
Returns the permissions available to this flag type. Overrides FlagTypeBase:: |
1 |
EntityFlagType:: |
public static | function |
Creates an instance of the plugin. Overrides FlagTypeBase:: |
|
EntityFlagType:: |
public | function | Returns the show as field setting. | |
EntityFlagType:: |
public | function | Returns the show on contextual link setting. | |
EntityFlagType:: |
public | function | Return the show in links setting given a view mode. | |
EntityFlagType:: |
public | function | Returns the show on form setting. | |
EntityFlagType:: |
public | function |
Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides FlagTypeBase:: |
|
FlagTypeBase:: |
protected | property | The module handler. | |
FlagTypeBase:: |
public | function | ||
FlagTypeBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurableInterface:: |
|
FlagTypeBase:: |
protected | function | Determines whether the flag is set to have the extra permissions set. | |
FlagTypeBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurableInterface:: |
|
FlagTypeBase:: |
public | function |
Handles the validation for the action link plugin settings form. Overrides PluginFormInterface:: |
|
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:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
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. | |
UserFlagType:: |
public | function |
Checks whether a user has permission to flag/unflag or not. Overrides EntityFlagType:: |
|
UserFlagType:: |
public | function |
Provides a form for this action link plugin settings. Overrides EntityFlagType:: |
|
UserFlagType:: |
public | function |
Gets default configuration for this plugin. Overrides EntityFlagType:: |
|
UserFlagType:: |
protected | function |
Defines options for extra permissions. Overrides EntityFlagType:: |
|
UserFlagType:: |
protected | function |
Defines permissions for the 'owner' set of additional action permissions. Overrides EntityFlagType:: |
|
UserFlagType:: |
public | function |
Determines if the given form operation is add or edit. Overrides EntityFlagType:: |
|
UserFlagType:: |
protected | function |
Determines if the flaggable associated with the flag supports ownership. Overrides EntityFlagType:: |
|
UserFlagType:: |
public | function | Specifies if the flag link should appear on the user profile. | |
UserFlagType:: |
public | function |
Handles the form submit for this action link plugin. Overrides EntityFlagType:: |