You are here

function mass_contact_opt_out_allowed_values_callback in Mass Contact 8

Implements callback_allowed_values_function().

1 string reference to 'mass_contact_opt_out_allowed_values_callback'
field.storage.user.mass_contact_opt_out.yml in config/install/field.storage.user.mass_contact_opt_out.yml
config/install/field.storage.user.mass_contact_opt_out.yml

File

./mass_contact.module, line 132
This is the main code file for the Mass Contact module.

Code

function mass_contact_opt_out_allowed_values_callback(FieldStorageDefinitionInterface $definition, FieldableEntityInterface $entity = NULL, &$cacheable = NULL) {
  $opt_out = \Drupal::config('mass_contact.settings')
    ->get('optout_enabled');
  if ($opt_out === MassContactInterface::OPT_OUT_DISABLED) {

    // Opt-out is disabled.
    return [];
  }
  $values = [
    1 => t('Opt-out of all emails'),
  ];
  if ($opt_out === MassContactInterface::OPT_OUT_GLOBAL) {

    // Return a simple global opt-out checkbox.
    return $values;
  }

  // Per-category opt-out is enabled, so return all categories.

  /** @var \Drupal\mass_contact\Entity\MassContactCategoryInterface[] $categories */
  $categories = \Drupal::entityTypeManager()
    ->getStorage('mass_contact_category')
    ->loadMultiple();
  foreach ($categories as $category) {
    $values[$category
      ->id()] = $category
      ->label();
  }
  return $values;
}