You are here

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

Expanded class hierarchy of UserFlagType

File

src/Plugin/Flag/UserFlagType.php, line 21

Namespace

Drupal\flag\Plugin\Flag
View 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

Namesort descending Modifiers Type Description Overrides
EntityFlagType::$entityType protected property The entity type defined in plugin definition.
EntityFlagType::$entityTypeManager protected property The entity type manager.
EntityFlagType::actionPermissions public function Returns the permissions available to this flag type. Overrides FlagTypeBase::actionPermissions 1
EntityFlagType::create public static function Creates an instance of the plugin. Overrides FlagTypeBase::create
EntityFlagType::showAsField public function Returns the show as field setting.
EntityFlagType::showContextualLink public function Returns the show on contextual link setting.
EntityFlagType::showInLinks public function Return the show in links setting given a view mode.
EntityFlagType::showOnForm public function Returns the show on form setting.
EntityFlagType::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides FlagTypeBase::__construct
FlagTypeBase::$moduleHandler protected property The module handler.
FlagTypeBase::calculateDependencies public function
FlagTypeBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
FlagTypeBase::hasExtraPermission protected function Determines whether the flag is set to have the extra permissions set.
FlagTypeBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
FlagTypeBase::validateConfigurationForm public function Handles the validation for the action link plugin settings form. Overrides PluginFormInterface::validateConfigurationForm
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UserFlagType::actionAccess public function Checks whether a user has permission to flag/unflag or not. Overrides EntityFlagType::actionAccess
UserFlagType::buildConfigurationForm public function Provides a form for this action link plugin settings. Overrides EntityFlagType::buildConfigurationForm
UserFlagType::defaultConfiguration public function Gets default configuration for this plugin. Overrides EntityFlagType::defaultConfiguration
UserFlagType::getExtraPermissionsOptions protected function Defines options for extra permissions. Overrides EntityFlagType::getExtraPermissionsOptions
UserFlagType::getExtraPermissionsOwner protected function Defines permissions for the 'owner' set of additional action permissions. Overrides EntityFlagType::getExtraPermissionsOwner
UserFlagType::isAddEditForm public function Determines if the given form operation is add or edit. Overrides EntityFlagType::isAddEditForm
UserFlagType::isFlaggableOwnable protected function Determines if the flaggable associated with the flag supports ownership. Overrides EntityFlagType::isFlaggableOwnable
UserFlagType::showOnProfile public function Specifies if the flag link should appear on the user profile.
UserFlagType::submitConfigurationForm public function Handles the form submit for this action link plugin. Overrides EntityFlagType::submitConfigurationForm