1 taxonomy.install taxonomy_update_1001()

Convert taxonomy vocabularies to configuration.

Related topics

File

core/modules/taxonomy/taxonomy.install, line 226
Install, update and uninstall functions for the taxonomy module.

Code

function taxonomy_update_1001() {
  // Create the new machine name column in the term data table.
  if (!db_field_exists('taxonomy_term_data', 'vocabulary')) {
    db_add_field('taxonomy_term_data', 'vocabulary', array(
      'type' => 'varchar',
      'length' => 255,
      'not null' => TRUE,
      'default' => '',
      'description' => 'The vocabulary machine name to which this term is assigned.',
    ));
  }

  // Convert the vocabularies.
  if (db_table_exists('taxonomy_vocabulary')) {
    $result = db_query("SELECT * FROM {taxonomy_vocabulary}");
    foreach ($result as $row) {
      $vocabulary_data = (array) $row;
      $vid = $vocabulary_data['vid'];
      $machine_name = $vocabulary_data['machine_name'];
      unset($vocabulary_data['vid']);
      unset($vocabulary_data['module']);

      // Save the vocabulary as config.
      $config = config('taxonomy.vocabulary.' . $machine_name);
      $config->setData($vocabulary_data);
      $config->save();

      // Update the term data table.
      db_update('taxonomy_term_data')
        ->fields(array(
          'vocabulary' => $machine_name
        ))
        ->condition('vid', $vid)
        ->execute();

      // Update permission names to use machine name.
      db_update('role_permission')
        ->fields(array(
          'permission' => 'edit terms in ' . $machine_name
        ))
        ->condition('permission', 'edit terms in ' . $vid)
        ->execute();
      db_update('role_permission')
        ->fields(array(
          'permission' => 'delete terms in ' . $machine_name
        ))
        ->condition('permission', 'delete terms in ' . $vid)
        ->execute();
    }
  }
}