You are here

function advpoll_update_8001 in Advanced Poll 8

Add availability of the multiple write-in options.

File

./advpoll.install, line 54

Code

function advpoll_update_8001() {
  $configPath = drupal_get_path('module', 'advpoll') . '/config/install';
  $source = new FileStorage($configPath);
  $config_manager = Drupal::service('config.manager');
  $entityTypeManager = Drupal::entityTypeManager();
  $field_storage = $entityTypeManager
    ->getStorage('field_storage_config');
  $field_config = $entityTypeManager
    ->getStorage('field_config');

  // Only try to create the field if it doesn't already exist.
  if (!$field_storage
    ->load('poll.field_writein_multiple')) {
    $config_record = $source
      ->read('field.storage.poll.field_writein_multiple');
    $entity_type = $config_manager
      ->getEntityTypeIdByName('field.storage.poll.field_writein_multiple');

    /** @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface $storage */
    $storage = $entityTypeManager
      ->getStorage($entity_type);

    // Create the config entity.
    $entity = $storage
      ->createFromStorageRecord($config_record)
      ->save();
  }
  if (!$field_config
    ->load('poll.poll.field_writein_multiple')) {
    $config_record = $source
      ->read('field.field.poll.poll.field_writein_multiple');
    $entity_type = $config_manager
      ->getEntityTypeIdByName('field.field.poll.poll.field_writein_multiple');

    /** @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface $storage */
    $storage = $entityTypeManager
      ->getStorage($entity_type);

    // Create the config entity.
    $entity = $storage
      ->createFromStorageRecord($config_record)
      ->save();
  }
}