You are here

class HideOnRole in Field Formatter Condition 8

The plugin for check empty fields.

Plugin annotation


@FieldFormatterCondition(
  id = "hide_on_role",
  label = @Translation("Hide when current user has role"),
  dsFields = TRUE,
  types = {
    "all"
  }
)

Hierarchy

Expanded class hierarchy of HideOnRole

File

src/Plugin/Field/FieldFormatter/Condition/HideOnRole.php, line 19

Namespace

Drupal\fico\Plugin\Field\FieldFormatter\Condition
View source
class HideOnRole extends FieldFormatterConditionBase {

  /**
   * {@inheritdoc}
   */
  public function alterForm(&$form, $settings) {
    $user_roles = [];
    foreach (Role::loadMultiple() as $role) {
      $user_roles[$role
        ->id()] = $role
        ->label();
    }
    $default_include = isset($settings['settings']['include_admin']) ? $settings['settings']['include_admin'] : NULL;
    $default_roles = isset($settings['settings']['roles']) ? $settings['settings']['roles'] : NULL;
    $form['include_admin'] = array(
      '#type' => 'checkbox',
      '#title' => t('Include the administrator'),
      '#default_value' => $default_include,
    );
    $form['roles'] = array(
      '#type' => 'select',
      '#multiple' => TRUE,
      '#title' => t('Select roles'),
      '#options' => $user_roles,
      '#default_value' => $default_roles,
    );
  }

  /**
   * {@inheritdoc}
   */
  public function access(&$build, $field, $settings) {
    if (array_intersect(\Drupal::currentUser()
      ->getRoles(), $settings['settings']['roles']) && (\Drupal::currentUser()
      ->id() != 1 || $settings['settings']['include_admin'] == 1)) {
      $build[$field]['#access'] = FALSE;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function summary($settings) {
    $roles = [];
    foreach (Role::loadMultiple() as $role) {
      if (in_array($role
        ->id(), $settings['settings']['roles'])) {
        $roles[] = $role
          ->label();
      }
    }
    return t("Condition: %condition (%settings)", [
      "%condition" => t('Hide when current user has role'),
      '%settings' => implode(', ', $roles),
    ]);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FieldFormatterConditionBase::createInstance public function Creates a pre-configured instance of a plugin. Overrides FactoryInterface::createInstance
FieldFormatterConditionBase::getDefinition public function Gets a specific plugin definition. Overrides DiscoveryInterface::getDefinition
FieldFormatterConditionBase::getDefinitions public function Gets the definition of all plugins for this type. Overrides DiscoveryInterface::getDefinitions
FieldFormatterConditionBase::getEntity protected function Check for entity in build.
FieldFormatterConditionBase::getEntityFields protected function Load fields from a entity.
FieldFormatterConditionBase::getEntityType protected function Check for entity_type in build.
FieldFormatterConditionBase::getInstance public function Gets a preconfigured instance of a plugin. Overrides MapperInterface::getInstance
FieldFormatterConditionBase::hasDefinition public function Indicates if a specific plugin definition exists. Overrides DiscoveryInterface::hasDefinition
HideOnRole::access public function Access control function. Overrides FieldFormatterConditionBase::access
HideOnRole::alterForm public function Alter the condition form. Overrides FieldFormatterConditionBase::alterForm
HideOnRole::summary public function Return the summary string. Overrides FieldFormatterConditionBase::summary
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.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. 92