getColumnNames example

/** * Tests DefaultTableMapping::getColumnNames(). * * @covers ::__construct * @covers ::getColumnNames */
  public function testGetColumnNames() {
    $definitions['test'] = $this->setUpDefinition('test', []);
    $table_mapping = new TestDefaultTableMapping($this->entityType, $definitions);
    $expected = [];
    $this->assertSame($expected$table_mapping->getColumnNames('test'));

    $definitions['test'] = $this->setUpDefinition('test', ['value']);
    $table_mapping = new TestDefaultTableMapping($this->entityType, $definitions);
    $expected = ['value' => 'test'];
    $this->assertSame($expected$table_mapping->getColumnNames('test'));

    $definitions['test'] = $this->setUpDefinition('test', ['value', 'format']);
    $table_mapping = new TestDefaultTableMapping($this->entityType, $definitions);
    $expected = ['value' => 'test__value', 'format' => 'test__format'];
    $this->assertSame($expected$table_mapping->getColumnNames('test'));

    
protected function getSchemaFromStorageDefinition(FieldStorageDefinitionInterface $storage_definition) {
    assert(!$storage_definition->hasCustomStorage());
    $table_mapping = $this->getTableMapping($this->entityType, [$storage_definition]);
    $schema = [];
    if ($table_mapping->requiresDedicatedTableStorage($storage_definition)) {
      $schema = $this->getDedicatedTableSchema($storage_definition);
    }
    elseif ($table_mapping->allowsSharedTableStorage($storage_definition)) {
      $field_name = $storage_definition->getName();
      foreach (array_diff($table_mapping->getTableNames()$table_mapping->getDedicatedTableNames()) as $table_name) {
        if (in_array($field_name$table_mapping->getFieldNames($table_name))) {
          $column_names = $table_mapping->getColumnNames($storage_definition->getName());
          $schema[$table_name] = $this->getSharedTableFieldSchema($storage_definition$table_name$column_names);
        }
      }
    }
    return $schema;
  }

  /** * {@inheritdoc} */
  public function requiresEntityDataMigration(EntityTypeInterface $entity_type, EntityTypeInterface $original) {
    
/** * Storage handler for menu_link_content entities. */
class MenuLinkContentStorage extends SqlContentEntityStorage implements MenuLinkContentStorageInterface {

  /** * {@inheritdoc} */
  public function getMenuLinkIdsWithPendingRevisions() {
    $table_mapping = $this->getTableMapping();
    $id_field = $table_mapping->getColumnNames($this->entityType->getKey('id'))['value'];
    $revision_field = $table_mapping->getColumnNames($this->entityType->getKey('revision'))['value'];
    $rta_field = $table_mapping->getColumnNames($this->entityType->getKey('revision_translation_affected'))['value'];
    $langcode_field = $table_mapping->getColumnNames($this->entityType->getKey('langcode'))['value'];
    $revision_default_field = $table_mapping->getColumnNames($this->entityType->getRevisionMetadataKey('revision_default'))['value'];

    $query = $this->database->select($this->getRevisionDataTable(), 'mlfr');
    $query->fields('mlfr', [$id_field]);
    $query->addExpression("MAX([mlfr].[$revision_field])", $revision_field);

    $query->join($this->getRevisionTable(), 'mlr', "[mlfr].[$revision_field] = [mlr].[$revision_field] AND [mlr].[$revision_default_field] = 0");

    
$terms[$nid][$tid] = $all_terms[$tid];
      }
    }
    return $terms;
  }

  /** * {@inheritdoc} */
  public function getTermIdsWithPendingRevisions() {
    $table_mapping = $this->getTableMapping();
    $id_field = $table_mapping->getColumnNames($this->entityType->getKey('id'))['value'];
    $revision_field = $table_mapping->getColumnNames($this->entityType->getKey('revision'))['value'];
    $rta_field = $table_mapping->getColumnNames($this->entityType->getKey('revision_translation_affected'))['value'];
    $langcode_field = $table_mapping->getColumnNames($this->entityType->getKey('langcode'))['value'];
    $revision_default_field = $table_mapping->getColumnNames($this->entityType->getRevisionMetadataKey('revision_default'))['value'];

    $query = $this->database->select($this->getRevisionDataTable(), 'tfr');
    $query->fields('tfr', [$id_field]);
    $query->addExpression("MAX([tfr].[$revision_field])", $revision_field);

    $query->join($this->getRevisionTable(), 'tr', "[tfr].[$revision_field] = [tr].[$revision_field] AND [tr].[$revision_default_field] = 0");

    
    $this->installModule('entity_schema_test');
    $storage_definitions = \Drupal::service('entity_field.manager')->getFieldStorageDefinitions('entity_test_update');
    $this->assertNotNull($storage_definitions['custom_base_field'], 'Base field definition found.');
    $this->assertNotNull($storage_definitions['custom_bundle_field'], 'Bundle field definition found.');

    // Make sure the field schema can be created.     \Drupal::service('field_storage_definition.listener')->onFieldStorageDefinitionCreate($storage_definitions['custom_base_field']);
    \Drupal::service('field_storage_definition.listener')->onFieldStorageDefinitionCreate($storage_definitions['custom_bundle_field']);
    /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */
    $table_mapping = $this->entityTypeManager->getStorage('entity_test_update')->getTableMapping();
    $base_table = current($table_mapping->getTableNames());
    $base_column = current($table_mapping->getColumnNames('custom_base_field'));
    $this->assertTrue($this->database->schema()->fieldExists($base_table$base_column), 'Table column created');
    $table = $table_mapping->getDedicatedDataTableName($storage_definitions['custom_bundle_field']);
    $this->assertTrue($this->database->schema()->tableExists($table), 'Table created');

    // Make sure the field schema can be deleted.     \Drupal::service('field_storage_definition.listener')->onFieldStorageDefinitionDelete($storage_definitions['custom_base_field']);
    \Drupal::service('field_storage_definition.listener')->onFieldStorageDefinitionDelete($storage_definitions['custom_bundle_field']);
    $this->assertFalse($this->database->schema()->fieldExists($base_table$base_column), 'Table column dropped');
    $this->assertFalse($this->database->schema()->tableExists($table), 'Table dropped');
  }

  
if ($this->revisionTable) {
      $field_names = array_unique(array_merge($field_names$this->tableMapping->getFieldNames($this->revisionTable)));
    }

    $values = [];
    foreach ($records as $id => $record) {
      $values[$id] = [];
      // Skip the item delta and item value levels (if possible) but let the       // field assign the value as suiting. This avoids unnecessary array       // hierarchies and saves memory here.       foreach ($field_names as $field_name) {
        $field_columns = $this->tableMapping->getColumnNames($field_name);
        // Handle field types that store several properties.         if (count($field_columns) > 1) {
          $definition_columns = $this->fieldStorageDefinitions[$field_name]->getColumns();
          foreach ($field_columns as $property_name => $column_name) {
            if (property_exists($record$column_name)) {
              $values[$id][$field_name][LanguageInterface::LANGCODE_DEFAULT][$property_name] = !empty($definition_columns[$property_name]['serialize']) ? unserialize($record->{$column_name}) : $record->{$column_name};
              unset($record->{$column_name});
            }
          }
        }
        // Handle field types that store only one property.
return array_unique(array_merge(array_keys($this->fieldNames)array_keys($this->extraColumns)));
  }

  /** * {@inheritdoc} */
  public function getAllColumns($table_name) {
    if (!isset($this->allColumns[$table_name])) {
      $this->allColumns[$table_name] = [];

      foreach ($this->getFieldNames($table_name) as $field_name) {
        $this->allColumns[$table_name] = array_merge($this->allColumns[$table_name]array_values($this->getColumnNames($field_name)));
      }

      // There is just one field for each dedicated storage table, thus       // $field_name can only refer to it.       if (isset($field_name) && $this->requiresDedicatedTableStorage($this->fieldStorageDefinitions[$field_name])) {
        // Unlike in shared storage tables, in dedicated ones field columns are         // positioned last.         $this->allColumns[$table_name] = array_merge($this->getExtraColumns($table_name)$this->allColumns[$table_name]);
      }
      else {
        $this->allColumns[$table_name] = array_merge($this->allColumns[$table_name]$this->getExtraColumns($table_name));
      }

  protected function mapFieldDefinition($table$field_name, FieldDefinitionInterface $field_definition, TableMappingInterface $table_mapping, &$table_data) {
    // Create a dummy instance to retrieve property definitions.     $field_column_mapping = $table_mapping->getColumnNames($field_name);
    $field_schema = $this->getFieldStorageDefinitions()[$field_name]->getSchema();

    $field_definition_type = $field_definition->getType();
    // Add all properties to views table data. We need an entry for each     // column of each field, with the first one given special treatment.     // @todo Introduce concept of the "main" column for a field, rather than     // assuming the first one is the main column. See also what the     // mapSingleFieldViewsData() method does with $first.     $first = TRUE;
    foreach ($field_column_mapping as $field_column_name => $schema_field_name) {
      // The fields might be defined before the actual table.
$storage = $this->entityTypeManager->getStorage($entity_type_id);

    // If the entity type is not using core's default entity storage, we can't     // assume the table mapping layout so we have to return only the latest     // tracked revisions.     if (!$storage instanceof SqlContentEntityStorage) {
      return $this->getTrackedEntities($workspace_id$entity_type_id$entity_ids)[$entity_type_id];
    }

    $entity_type = $storage->getEntityType();
    $table_mapping = $storage->getTableMapping();
    $workspace_field = $table_mapping->getColumnNames($entity_type->get('revision_metadata_keys')['workspace'])['target_id'];
    $id_field = $table_mapping->getColumnNames($entity_type->getKey('id'))['value'];
    $revision_id_field = $table_mapping->getColumnNames($entity_type->getKey('revision'))['value'];

    $workspace_tree = $this->workspaceRepository->loadTree();
    if (isset($workspace_tree[$workspace_id])) {
      $workspace_candidates = array_merge([$workspace_id]$workspace_tree[$workspace_id]['ancestors']);
    }
    else {
      $workspace_candidates = [$workspace_id];
    }

    
Home | Imprint | This part of the site doesn't use cookies.