You are here

abstract class FlagTypeBase in Flag 8.4

Provides a base class for flag type plugins.

Hierarchy

Expanded class hierarchy of FlagTypeBase

1 file declares its use of FlagTypeBase
EntityFlagType.php in src/Plugin/Flag/EntityFlagType.php

File

src/FlagType/FlagTypeBase.php, line 20

Namespace

Drupal\flag\FlagType
View source
abstract class FlagTypeBase extends PluginBase implements FlagTypePluginInterface {
  use StringTranslationTrait;

  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * {@inheritdoc}
   */
  public function __construct(array $configuration, $plugin_id, array $plugin_definition, ModuleHandlerInterface $module_handler, TranslationInterface $string_translation) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->moduleHandler = $module_handler;
    $this->configuration += $this
      ->defaultConfiguration();
    $this->stringTranslation = $string_translation;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('module_handler'), $container
      ->get('string_translation'));
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [];
  }

  /**
   * {@inheritdoc}
   */
  public function calculateDependencies() {
    return [];
  }

  /**
   * {@inheritdoc}
   */
  public function getConfiguration() {
    return $this->configuration;
  }

  /**
   * {@inheritdoc}
   */
  public function setConfiguration(array $configuration) {
    $this->configuration = NestedArray::mergeDeep($this
      ->defaultConfiguration(), $configuration);
  }

  /**
   * Provides a form for this action link plugin settings.
   *
   * The form provided by this method is displayed by the FlagAddForm when
   * creating or editing the Flag. Derived classes should override this.
   *
   * @param array $form
   *   The form array.
   * @param FormStateInterface $form_state
   *   The form state.
   *
   * @return array
   *   The form array
   *
   * @see \Drupal\flag\Form\FlagAddForm
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    return $form;
  }

  /**
   * Handles the form submit for this action link plugin.
   *
   * Derived classes will want to override this.
   *
   * @param array $form
   *   The form array.
   * @param FormStateInterface $form_state
   *   The form state.
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {

    // Override this.
  }

  /**
   * Handles the validation for the action link plugin settings form.
   *
   * @param array $form
   *   The form array.
   * @param FormStateInterface $form_state
   *   The form state.
   */
  public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {

    // Override this.
  }

  /**
   * Defines options for extra permissions.
   *
   * @return array
   *   An array of options suitable for FormAPI.
   */
  protected function getExtraPermissionsOptions() {
    return [];
  }

  /**
   * Determines whether the flag is set to have the extra permissions set.
   *
   * @param string $option
   *   The name of an extra permissions set. These are defined in
   *   getExtraPermissionsOptions().
   *
   * @return bool
   *   TRUE if the flag is configured to have the permissions, FALSE if not.
   */
  protected function hasExtraPermission($option) {
    return in_array($option, $this->configuration['extra_permissions']);
  }

  /**
   * {@inheritdoc}
   */
  public function actionPermissions(FlagInterface $flag) {
    return [
      'flag ' . $flag
        ->id() => [
        'title' => $this
          ->t('Flag %flag_title', [
          '%flag_title' => $flag
            ->label(),
        ]),
      ],
      'unflag ' . $flag
        ->id() => [
        'title' => $this
          ->t('Unflag %flag_title', [
          '%flag_title' => $flag
            ->label(),
        ]),
      ],
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function actionAccess($action, FlagInterface $flag, AccountInterface $account, EntityInterface $flaggable = NULL) {

    // Collect access results from objects.
    $results = $this->moduleHandler
      ->invokeAll('flag_action_access', [
      $action,
      $flag,
      $account,
      $flaggable,
    ]);

    // Add default access check.
    $results[] = AccessResult::allowedIfHasPermission($account, $action . ' ' . $flag
      ->id());

    /** @var \Drupal\Core\Access\AccessResultInterface $return */
    $return = array_shift($results);
    foreach ($results as $next) {
      $return = $return
        ->orIf($next);
    }
    return $return;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FlagTypeBase::$moduleHandler protected property The module handler.
FlagTypeBase::actionAccess public function Checks whether a user has permission to flag/unflag or not. Overrides FlagTypePluginInterface::actionAccess 1
FlagTypeBase::actionPermissions public function Returns the permissions available to this flag type. Overrides FlagTypePluginInterface::actionPermissions 1
FlagTypeBase::buildConfigurationForm public function Provides a form for this action link plugin settings. Overrides PluginFormInterface::buildConfigurationForm 1
FlagTypeBase::calculateDependencies public function
FlagTypeBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create 1
FlagTypeBase::defaultConfiguration public function Gets default configuration for this plugin. Overrides ConfigurableInterface::defaultConfiguration 1
FlagTypeBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
FlagTypeBase::getExtraPermissionsOptions protected function Defines options for extra permissions. 1
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::submitConfigurationForm public function Handles the form submit for this action link plugin. Overrides PluginFormInterface::submitConfigurationForm 1
FlagTypeBase::validateConfigurationForm public function Handles the validation for the action link plugin settings form. Overrides PluginFormInterface::validateConfigurationForm
FlagTypeBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct 1
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.