You are here

public function RequiredByRole::isRequired in Required by role 8

Determines wether a field is required or not.

Parameters

\Drupal\Core\Field\FieldDefinitionInterface $field: An field instance object.

\Drupal\Core\Session\AccountInterface $account: An account object.

Return value

bool TRUE on required. FALSE otherwise.

File

src/Plugin/Required/RequiredByRole.php, line 37
Contains \Drupal\required_by_role\Plugin\Required\RequiredByRole.

Class

RequiredByRole
Plugin annotation @Required( id = "required_by_role", admin_label = @Translation("Required by role"), label = @Translation("Required by role"), description = @Translation("Required based on current user roles.") )

Namespace

Drupal\required_by_role\Plugin\Required

Code

public function isRequired(FieldDefinitionInterface $field, AccountInterface $account) {
  $available_roles = $account
    ->getRoles();
  $field_roles = $field
    ->getThirdPartySetting('required_api', 'required_plugin_options', []);
  $is_required = $this
    ->getMatches($available_roles, $field_roles);
  return $is_required;
}