You are here

function viewfield_update_8302 in Viewfield 8.3

Add field for items_to_display.

File

./viewfield.install, line 51
Install, update and uninstall functions for the viewfield module.

Code

function viewfield_update_8302() {
  $property = 'items_to_display';
  $db_type = 'varchar';
  $db_schema = Database::getConnection()
    ->schema();
  if (!($field_storage_configs = \Drupal::entityTypeManager()
    ->getStorage('field_storage_config')
    ->loadByProperties([
    'type' => 'viewfield',
  ]))) {
    return;
  }
  foreach ($field_storage_configs as $field_storage) {

    /** @var \Drupal\Core\Field\FieldStorageDefinitionInterface $field_storage */
    $schema = $field_storage
      ->getSchema();
    $field_spec = $schema['columns'][$property];
    $field_spec['type'] = $db_type;
    $entity_type_id = $field_storage
      ->getTargetEntityTypeId();

    /** @var \Drupal\Core\Entity\Sql\SqlEntityStorageInterface $entity_storage */
    $entity_storage = \Drupal::entityTypeManager()
      ->getStorage($entity_type_id);

    /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */
    $table_mapping = $entity_storage
      ->getTableMapping();
    $tables = [
      $table_mapping
        ->getDedicatedDataTableName($field_storage),
    ];
    if ($field_storage
      ->isRevisionable()) {
      $tables[] = $table_mapping
        ->getDedicatedRevisionTableName($field_storage);
    }
    $column = $table_mapping
      ->getFieldColumnName($field_storage, $property);
    foreach ($tables as $table) {
      if ($db_schema
        ->tableExists($table)) {
        $db_schema
          ->addField($table, $column, $field_spec);
      }
    }
  }
}