You are here

function rng_add_event_field_storage in RNG - Events and Registrations 8

Same name and namespace in other branches
  1. 8.2 rng.field.defaults.inc \rng_add_event_field_storage()
  2. 3.x rng.field.defaults.inc \rng_add_event_field_storage()
2 calls to rng_add_event_field_storage()
EventType::postSave in src/Entity/EventType.php
Acts on a saved entity before the insert or update hook is invoked.
EventTypeFieldMappingForm::createField in src/Form/EventTypeFieldMappingForm.php
Form submission function to respond to the create field button.

File

./rng.field.defaults.inc, line 16
Creates field config if they do not exist.

Code

function rng_add_event_field_storage($field_name, $entity_type) {
  if ($field_storage = FieldStorageConfig::loadByName($entity_type, $field_name)) {
    return NULL;
  }
  $definition = array();
  switch ($field_name) {
    case EventManagerInterface::FIELD_REGISTRATION_TYPE:
      $definition = array(
        'type' => 'entity_reference',
        'settings' => array(
          'target_type' => 'registration_type',
        ),
        'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
      );
      break;
    case EventManagerInterface::FIELD_REGISTRATION_GROUPS:
      $definition = array(
        'type' => 'entity_reference',
        'settings' => array(
          'target_type' => 'registration_group',
        ),
        'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
      );
      break;
    case EventManagerInterface::FIELD_STATUS:
      $definition = array(
        'type' => 'boolean',
      );
      break;
    case EventManagerInterface::FIELD_CAPACITY:
      $definition = array(
        'type' => 'integer',
      );
      break;
    case EventManagerInterface::FIELD_EMAIL_REPLY_TO:
      $definition = array(
        'type' => 'email',
      );
      break;
    case EventManagerInterface::FIELD_ALLOW_DUPLICATE_REGISTRANTS:
      $definition = array(
        'type' => 'boolean',
      );
      break;
    case EventManagerInterface::FIELD_REGISTRATION_REGISTRANTS_MINIMUM:
      $definition = [
        'type' => 'integer',
      ];
      break;
    case EventManagerInterface::FIELD_REGISTRATION_REGISTRANTS_MAXIMUM:
      $definition = [
        'type' => 'integer',
      ];
      break;
  }
  if (!empty($definition)) {
    $field_storage = entity_create('field_storage_config', array(
      'field_name' => $field_name,
      'entity_type' => $entity_type,
    ) + $definition);
    $field_storage
      ->save();
  }
}