drowl_paragraphs-8.x-3.9/drowl_paragraphs.install

drowl_paragraphs.install
<?php

/**
 * Add further columns: style_textalign, style_textcolumns
 */
function drowl_paragraphs_update_8003() {
  $field_type = 'drowl_paragraphs_settings';
  $columns_to_add = ['style_textalign', 'style_textcolumns'];
  return _field_type_schema_column_add_helper($field_type, $columns_to_add);
}

/**
 * Set layout_XX_columns UNSIGNED = FALSE.
 */
function drowl_paragraphs_update_8004() {
  $field_type = 'drowl_paragraphs_settings';
  $columns_changed = ['layout_sm_columns', 'layout_md_columns', 'layout_ld_columns'];
  return _field_type_schema_column_spec_change_helper($field_type, $columns_changed);
}

/**
 * Add further columns: style_cutline
 */
function drowl_paragraphs_update_8005() {
  $field_type = 'drowl_paragraphs_settings';
  $columns_to_add = ['style_cutline'];
  return _field_type_schema_column_add_helper($field_type, $columns_to_add);
}

/**
 * Add further columns: layout_sm_collapse, layout_md_collapse, layout_lg_collapse
 */
function drowl_paragraphs_update_8006() {
  $field_type = 'drowl_paragraphs_settings';
  $columns_to_add = ['layout_sm_collapse', 'layout_md_collapse', 'layout_lg_collapse'];
  return _field_type_schema_column_add_helper($field_type, $columns_to_add);
}

/**
 * Migrate field_paragraph_settings_layout_reverse_order into a field like in 4.x (#3260848)
 */
// function drowl_paragraphs_update_8007(&$sandbox) {
//   // TODO:
//   _drowl_paragraphs_update_layout_reverse_order_to_field($sandbox);
// }

/**
 * Helper function to create field field_paragraphs_media_alignment in
 * types videos_text and image_text
 * and migrate the values from field_paragraph_settings_layout_reverse_order.
 *
 * @see https://www.drupal.org/project/drowl_paragraphs/issues/3260848
 *
 * @param [type] $sandbox
 */
function _drowl_paragraphs_update_layout_reverse_order_to_field(&$sandbox) {
  // TODO: Create field field_paragraphs_media_alignment in types videos_text and bild_text (1.x and 2.x ise the old name) and migrate the values from field_paragraph_settings_layout_reverse_order.
  // 0 = col_left
  // 1 = col_right
  $field_name = 'field_paragraphs_media_alignment';
  $paragraph_bundles = ['bild_text', 'video_text'];
  $field_storage = \Drupal\field\Entity\FieldStorageConfig::loadByName('paragraph', $field_name);
  if(!empty($field_storage)){
    // If the field already exists, return.
    return t('Field @field_name was not created as it is already existing.', ['@field_name', $field_name]);
  }

  // Otherwise create field and displays:
  \Drupal\field\Entity\FieldStorageConfig::create(array(
    'field_name' => $field_name,
    'entity_type' => 'paragraph',
    'type' => 'list_string',
    'cardinality' => 1,
    'translatable' => TRUE,
    'locked' => TRUE,
    'module' => 'options',
    'settings' => [
      'allowed_values' => [
        [
          'value' => 'col_left',
          'label' => 'Media left, text beside media',
        ],
        [
          'value' => 'col_right',
          'label' => 'Media right, text beside media',
        ],
        [
          'value' => 'float_left',
          'label' => 'Media left, text floats around media',
        ],
        [
          'value' => 'float_right',
          'label' => 'Media right, text floats around media',
        ],
      ],
      'allowed_values_function' => '',
    ],
  ))->save();

  foreach ($paragraph_bundles as $bundle) {
    \Drupal\field\Entity\FieldConfig::create([
      'field_name' => $field_name,
      'entity_type' => 'paragraph',
      'bundle' => $bundle,
      'label' => 'Alignment',
      'description' => 'Set how the medium is to be aligned in relation to the text.',
      'required' => TRUE,
      'translatable' => TRUE,
    ])->save();

    // Manage form display
    $form_display = \Drupal::service('entity_display.repository')->getFormDisplay('paragraph', $bundle);
    $form_display = $form_display->setComponent($field_name, ['type' => 'options_select', 'weight' => 3, 'region' => 'content']);
    $form_display->save();

    // Manage view display: Set hidden.
    $view_display = \Drupal::service('entity_display.repository')->getViewDisplay('paragraph', $bundle);
    $view_display->removeComponent($field_name);
    $view_display->save();
  }

  // TODO: Now loop through all entities of these bundles and convert the values from the old setting!
}

/**
 * Helper function for HOOK_Update to add columns to the field schema.
 *
 * @param $field_type The field type id e.g. "drowl_paragraphs_settings"
 * @param array $columns_to_add array of the column names from schema() function, e.g. ["style_textalign"]
 */
function _field_type_schema_column_add_helper($field_type, array $columns_to_add = array()) {
  $processed_fields = [];
  $field_type_manager = \Drupal::service('plugin.manager.field.field_type');
  $field_definition = $field_type_manager->getDefinition($field_type);
  $field_item_class = $field_definition['class'];

  $schema = \Drupal::database()->schema();
  $entity_type_manager = \Drupal::entityTypeManager();
  $entity_field_manager = \Drupal::service('entity_field.manager');
  $entity_field_map = $entity_field_manager->getFieldMapByFieldType($field_type);
  // The key-value collection for tracking installed storage schema.
  $entity_storage_schema_sql = \Drupal::keyValue('entity.storage_schema.sql');
  $entity_definitions_installed = \Drupal::keyValue('entity.definitions.installed');

  foreach ($entity_field_map as $entity_type_id => $field_map) {
    $entity_storage = $entity_type_manager->getStorage($entity_type_id);

    // Only SQL storage based entities are supported / throw known exception.
    //    if (!($entity_storage instanceof SqlContentEntityStorage)) {
    //      continue;
    //    }

    $entity_type = $entity_type_manager->getDefinition($entity_type_id);
    $field_storage_definitions = $entity_field_manager->getFieldStorageDefinitions($entity_type_id);
    /** @var Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */
    $table_mapping = $entity_storage->getTableMapping($field_storage_definitions);
    // Only need field storage definitions of address fields.
    /** @var \Drupal\Core\Field\FieldStorageDefinitionInterface $field_storage_definition */
    foreach (array_intersect_key($field_storage_definitions, $field_map) as $field_storage_definition) {
      $field_name = $field_storage_definition->getName();
      try {
        $table = $table_mapping->getFieldTableName($field_name);
      } catch (SqlContentEntityStorageException $e) {
        // Custom storage? Broken site? No matter what, if there is no table
        // or column, there's little we can do.
        continue;
      }
      // See if the field has a revision table.
      $revision_table = NULL;
      if ($entity_type->isRevisionable() && $field_storage_definition->isRevisionable()) {
        if ($table_mapping->requiresDedicatedTableStorage($field_storage_definition)) {
          $revision_table = $table_mapping->getDedicatedRevisionTableName($field_storage_definition);
        } elseif ($table_mapping->allowsSharedTableStorage($field_storage_definition)) {
          $revision_table = $entity_type->getRevisionDataTable() ?: $entity_type->getRevisionTable();
        }
      }
      // Load the installed field schema so that it can be updated.
      $schema_key = "$entity_type_id.field_schema_data.$field_name";
      $field_schema_data = $entity_storage_schema_sql->get($schema_key);

      $processed_fields[] = [$entity_type_id, $field_name];
      // Loop over each new column and add it as a schema column change.
      foreach ($columns_to_add as $column_id) {
        $column = $table_mapping->getFieldColumnName($field_storage_definition, $column_id);
        // Add `initial_from_field` to the new spec, as this will copy over
        // the entire data.
        $field_schema = $field_item_class::schema($field_storage_definition);
        $spec = $field_schema['columns'][$column_id];

        // Add the new column.
        $schema->addField($table, $column, $spec);
        if ($revision_table) {
          $schema->addField($revision_table, $column, $spec);
        }

        // Add the new column to the installed field schema.
        if ($field_schema_data) {
          $field_schema_data[$table]['fields'][$column] = $field_schema['columns'][$column_id];
          $field_schema_data[$table]['fields'][$column]['not null'] = FALSE;
          if ($revision_table) {
            $field_schema_data[$revision_table]['fields'][$column] = $field_schema['columns'][$column_id];
            $field_schema_data[$revision_table]['fields'][$column]['not null'] = FALSE;
          }
        }
      }

      // Save changes to the installed field schema.
      if ($field_schema_data) {
        $recipient_column = $table_mapping->getFieldColumnName($field_storage_definition, 'recipient');
        unset($field_schema_data[$table]['fields'][$recipient_column]);
        if ($revision_table) {
          unset($field_schema_data[$revision_table]['fields'][$recipient_column]);
        }
        $entity_storage_schema_sql->set($schema_key, $field_schema_data);
      }
      if ($table_mapping->allowsSharedTableStorage($field_storage_definition)) {
        $key = "$entity_type_id.field_storage_definitions";
        if ($definitions = $entity_definitions_installed->get($key)) {
          $definitions[$field_name] = $field_storage_definition;
          $entity_definitions_installed->set($key, $definitions);
        }
      }
    }
  }
}

/**
 * Helper function for HOOK_Update to update the field schema columns.
 * List the columns with changed schema values and the helper will handle
 * the content migration and entity update for you.
 *
 * @param $field_type The field type id e.g. "drowl_paragraphs_settings"
 * @param array $columns_to_add array of the column names from schema() function, e.g. ["style_textalign"]
 */
function _field_type_schema_column_spec_change_helper($field_type, $column) {
  $processed_fields = [];
  $field_type_manager = \Drupal::service('plugin.manager.field.field_type');
  $field_definition = $field_type_manager->getDefinition($field_type);
  $field_item_class = $field_definition['class'];

  $schema = \Drupal::database()->schema();
  $entity_type_manager = \Drupal::entityTypeManager();
  $entity_field_manager = \Drupal::service('entity_field.manager');
  $entity_field_map = $entity_field_manager->getFieldMapByFieldType($field_type);
  // The key-value collection for tracking installed storage schema.
  $entity_storage_schema_sql = \Drupal::keyValue('entity.storage_schema.sql');
  $entity_definitions_installed = \Drupal::keyValue('entity.definitions.installed');

  foreach ($entity_field_map as $entity_type_id => $field_map) {
    $entity_storage = $entity_type_manager->getStorage($entity_type_id);
    $entity_type = $entity_type_manager->getDefinition($entity_type_id);
    $field_storage_definitions = $entity_field_manager->getFieldStorageDefinitions($entity_type_id);
    /** @var Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */
    $table_mapping = $entity_storage->getTableMapping($field_storage_definitions);
    // Only need field storage definitions of address fields.
    /** @var \Drupal\Core\Field\FieldStorageDefinitionInterface $field_storage_definition */
    foreach (array_intersect_key($field_storage_definitions, $field_map) as $field_storage_definition) {
      $field_name = $field_storage_definition->getName();
      $tables = [];
      try {
        $table = $table_mapping->getFieldTableName($field_name);
        $tables[] = $table;
      } catch (SqlContentEntityStorageException $e) {
        // Custom storage? Broken site? No matter what, if there is no table
        // or column, there's little we can do.
        continue;
      }
      // See if the field has a revision table.
      $revision_table = NULL;
      if ($entity_type->isRevisionable() && $field_storage_definition->isRevisionable()) {
        if ($table_mapping->requiresDedicatedTableStorage($field_storage_definition)) {
          $revision_table = $table_mapping->getDedicatedRevisionTableName($field_storage_definition);
          $tables[] = $revision_table;
        } elseif ($table_mapping->allowsSharedTableStorage($field_storage_definition)) {
          $revision_table = $entity_type->getRevisionDataTable() ?: $entity_type->getRevisionTable();
          $tables[] = $revision_table;
        }
      }

      $database = \Drupal::database();
      $existing_data = [];
      foreach ($tables as $table) {
        // Get the old data.
        $existing_data[$table] = $database->select($table)
          ->fields($table)
          ->execute()
          ->fetchAll(PDO::FETCH_ASSOC);

        // Wipe it.
        $database->truncate($table)->execute();
      }

      $manager = \Drupal::entityDefinitionUpdateManager();
      $manager->updateFieldStorageDefinition($manager->getFieldStorageDefinition($field_name, $entity_type_id));


      // Restore the data.
      foreach ($tables as $table) {
        $insert_query = $database
          ->insert($table)
          ->fields(array_keys(end($existing_data[$table])));
        foreach ($existing_data[$table] as $row) {
          $insert_query->values(array_values($row));
        }
        $insert_query->execute();
      }
    }
  }
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc