View source
<?php
namespace Drupal\flag\Plugin\Flag;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\flag\FlagInterface;
use Drupal\Core\Form\FormStateInterface;
class UserFlagType extends EntityFlagType {
public function defaultConfiguration() {
$options = parent::defaultConfiguration();
$options += [
'show_on_profile' => TRUE,
];
return $options;
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$form['access']['bundles'] = [
'#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(),
'#weight' => -1,
];
return $form;
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::submitConfigurationForm($form, $form_state);
$this->configuration['show_on_profile'] = $form_state
->getValue([
'show_on_profile',
]);
}
protected function getExtraPermissionsOptions() {
$options = parent::getExtraPermissionsOptions();
$options['owner'] = $this
->t('Permissions for users to flag themselves.');
return $options;
}
public function showOnProfile() {
return $this->configuration['show_on_profile'];
}
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;
}
protected function isFlaggableOwnable() {
return TRUE;
}
public function isAddEditForm($operation) {
return in_array($operation, [
'register',
'default',
]);
}
public function actionAccess($action, FlagInterface $flag, AccountInterface $account, EntityInterface $flaggable = NULL) {
$access = parent::actionAccess($action, $flag, $account, $flaggable);
if ($flaggable && $this
->hasExtraPermission('owner')) {
$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);
$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;
}
}