You are here

function yoast_seo_update_8202 in Real-time SEO for Drupal 8.2

Add a title and description property to the yoast_seo field.

File

./yoast_seo.install, line 31
Install, update, and uninstall functions for the Real-Time SEO module.

Code

function yoast_seo_update_8202() {
  $field_type = 'yoast_seo';
  $add_properties = [
    'title',
    'description',
  ];
  $manager = \Drupal::entityDefinitionUpdateManager();
  $field_map = \Drupal::service('entity_field.manager')
    ->getFieldMapByFieldType($field_type);
  foreach ($field_map as $entity_type_id => $fields) {
    foreach (array_keys($fields) as $field_name) {
      $field_storage_definition = $manager
        ->getFieldStorageDefinition($field_name, $entity_type_id);
      $storage = \Drupal::entityTypeManager()
        ->getStorage($entity_type_id);
      if ($storage instanceof SqlContentEntityStorage) {
        $table_mapping = $storage
          ->getTableMapping([
          $field_name => $field_storage_definition,
        ]);
        $table_names = $table_mapping
          ->getDedicatedTableNames();
        $columns = $table_mapping
          ->getColumnNames($field_name);
        foreach ($table_names as $table_name) {
          $field_schema = $field_storage_definition
            ->getSchema();
          $schema = \Drupal::database()
            ->schema();
          foreach ($add_properties as $new_property) {
            $field_exists = $schema
              ->fieldExists($table_name, $columns[$new_property]);
            $table_exists = $schema
              ->tableExists($table_name);
            if (!$field_exists && $table_exists) {
              $schema
                ->addField($table_name, $columns[$new_property], $field_schema['columns'][$new_property]);
            }
          }
        }
      }
      $manager
        ->updateFieldStorageDefinition($field_storage_definition);
    }
  }
}