You are here

function social_topic_update_8907 in Open Social 10.1.x

Same name and namespace in other branches
  1. 10.3.x modules/social_features/social_topic/social_topic.install \social_topic_update_8907()
  2. 10.0.x modules/social_features/social_topic/social_topic.install \social_topic_update_8907()
  3. 10.2.x modules/social_features/social_topic/social_topic.install \social_topic_update_8907()

Create new field & field storage configuration for new topic type icon field.

File

modules/social_features/social_topic/social_topic.install, line 302
Install, update and uninstall functions for the social_topic module.

Code

function social_topic_update_8907(&$sandbox) {
  $config_file = drupal_get_path('module', 'social_topic') . '/config/static/social_topic_update_8907.yml';
  if (is_file($config_file)) {
    $sandbox['configs'] = Yaml::parse(file_get_contents($config_file));
    if (!isset($sandbox['total'])) {

      // Count the amount we need to add to cover batching..
      $sandbox['total'] = count($sandbox['configs']);
      $sandbox['current'] = 0;
    }
    $names = array_keys($sandbox['configs']);
    $name = $names[$sandbox['current']++];
    $data = $sandbox['configs'][$name];
    $parts = explode('.', $name);
    switch ($parts[0] . '.' . $parts[1]) {
      case 'field.storage':
        $entity_type = \Drupal::service('config.manager')
          ->getEntityTypeIdByName($name);

        /** @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface $storage */
        $storage = \Drupal::entityTypeManager()
          ->getStorage($entity_type);
        $entity = $storage
          ->createFromStorageRecord($data);
        $entity
          ->save();
        break;
      case 'field.field':
        $field_config = FieldConfig::loadByName($parts[2], $parts[3], $parts[4]);
        if ($field_config instanceof FieldConfigInterface) {
          $field_config
            ->setDescription($data);
        }
        else {
          $field_config = FieldConfig::create($data);
        }
        $field_config
          ->save();
        break;
      default:

        // Fallback similar to before.
        \Drupal::configFactory()
          ->getEditable($name)
          ->setData($data)
          ->save(TRUE);
    }
    $sandbox['#finished'] = $sandbox['current'] / $sandbox['total'];
  }
}