fields example

$langcode = $entity->getUntranslated()->language()->getId();
    $fields = [$this->fieldName, $this->untranslatableFieldName];
    /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */
    $table_mapping = \Drupal::entityTypeManager()->getStorage($entity_type)->getTableMapping();

    foreach ($fields as $field_name) {
      $field_storage = FieldStorageConfig::loadByName($entity_type$field_name);
      $table = $table_mapping->getDedicatedDataTableName($field_storage);

      $record = \Drupal::database()
        ->select($table, 'f')
        ->fields('f')
        ->condition('f.entity_id', $id)
        ->condition('f.revision_id', $id)
        ->execute()
        ->fetchObject();

      if ($record->langcode != $langcode) {
        $status = FALSE;
        break;
      }
    }

    
// Tests that locations of different types and arbitrary lengths can be     // added to a source string. Too long locations will be cut off.     $source_string = $this->buildSourceString();
    $source_string->addLocation('javascript', $this->randomString(8));
    $source_string->addLocation('configuration', $this->randomString(50));
    $source_string->addLocation('code', $this->randomString(100));
    $source_string->addLocation('path', $location = $this->randomString(300));
    $source_string->save();

    $rows = $this->container->get('database')->select('locales_location')
      ->fields('locales_location')
      ->condition('sid', $source_string->lid)
      ->execute()
      ->fetchAllAssoc('type');
    $this->assertCount(4, $rows);
    $this->assertEquals(substr($location, 0, 255)$rows['path']->name);
  }

  /** * Tests Search API loading multiple objects. */
  public function testStringSearchApi() {
    

class Box extends DrupalSqlBase {

  /** * {@inheritdoc} */
  public function query() {
    $query = $this->select('boxes', 'b')
      ->fields('b', ['bid', 'body', 'info', 'format']);
    $query->orderBy('b.bid');

    return $query;
  }

  /** * {@inheritdoc} */
  public function fields() {
    return [
      'bid' => $this->t('The numeric identifier of the block/box'),
      
/** * The join options between the node and the term node table. */
  const JOIN = '[tn].[vid] = [n].[vid]';

  /** * {@inheritdoc} */
  public function query() {
    $query = $this->select('term_node', 'tn')
      ->distinct()
      ->fields('tn', ['nid', 'vid'])
      ->fields('n', ['type']);
    // Because this is an inner join it enforces the current revision.     $query->innerJoin('term_data', 'td', '[td].[tid] = [tn].[tid] AND [td].[vid] = :vid', [':vid' => $this->configuration['vid']]);
    $query->innerJoin('node', 'n', static::JOIN);
    return $query;
  }

  /** * {@inheritdoc} */
  public function fields() {
    

  public function testContentAdminSort() {
    $this->drupalLogin($this->adminUser);

    $changed = REQUEST_TIME;
    $connection = Database::getConnection();
    foreach (['dd', 'aa', 'DD', 'bb', 'cc', 'CC', 'AA', 'BB'] as $prefix) {
      $changed += 1000;
      $node = $this->drupalCreateNode(['title' => $prefix . $this->randomMachineName(6)]);
      $connection->update('node_field_data')
        ->fields(['changed' => $changed])
        ->condition('nid', $node->id())
        ->execute();
    }

    // Test that the default sort by node.changed DESC actually fires properly.     $nodes_query = $connection->select('node_field_data', 'n')
      ->fields('n', ['title'])
      ->orderBy('changed', 'DESC')
      ->execute()
      ->fetchCol();

    
    $fields = \Drupal::entityTypeManager()->getStorage('field_config')->loadByProperties(['field_storage_uuid' => $field_storage->uuid(), 'deleted' => TRUE, 'include_deleted' => TRUE]);
    $this->assertCount(1, $fields, 'There is one deleted field');
    $field = $fields[$field->uuid()];
    $this->assertEquals($bundle$field->getTargetBundle(), 'The deleted field is for the correct bundle');

    // Check that the actual stored content did not change during delete.     /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */
    $table_mapping = $storage->getTableMapping();
    $table = $table_mapping->getDedicatedDataTableName($field_storage);
    $column = $table_mapping->getFieldColumnName($field_storage, 'value');
    $result = Database::getConnection()->select($table, 't')
      ->fields('t')
      ->execute();
    foreach ($result as $row) {
      $this->assertEquals($row->{$column}$this->entities[$row->entity_id]->{$field_name}->value);
    }

    // There are 0 entities of this bundle with non-deleted data.     $found = $storage
      ->getQuery()
      ->accessCheck(FALSE)
      ->condition('type', $bundle)
      ->condition("$field_name.deleted", 0)
      

class MigrateMissingDatabaseSource extends SqlBase {

  /** * {@inheritdoc} */
  public function query(): SelectInterface {
    $field_names = ['id'];
    $query = $this
      ->select('missing_database', 'm')
      ->fields('m', $field_names);
    return $query;
  }

  /** * {@inheritdoc} */
  public function fields(): array {
    $fields = [
      'id' => $this->t('ID'),
    ];

    
return [
      'id' => [
        'type' => 'integer',
      ],
    ];
  }

  /** * {@inheritdoc} */
  public function query() {
    return $this->select('source_table', 's')->fields('s', ['id']);
  }

}
/** * {@inheritdoc} */
  protected function setUp(): void {
    parent::setUp();
    $connection = Database::getConnection();

    // Enable the update_test_no_preexisting module by altering the     // core.extension configuration directly, so that the schema version     // information is missing.     $extensions = $connection->select('config')
      ->fields('config', ['data'])
      ->condition('name', 'core.extension')
      ->execute()
      ->fetchField();
    $extensions = unserialize($extensions);
    $connection->update('config')
      ->fields([
        'data' => serialize(array_merge_recursive($extensions['module' => ['update_test_no_preexisting' => 0]])),
      ])
      ->condition('name', 'core.extension')
      ->execute();
  }

  

  public function countDefaultLanguageRevisions(NodeInterface $node) {
    return $this->database->query('SELECT COUNT(*) FROM {' . $this->getRevisionDataTable() . '} WHERE [nid] = :nid AND [default_langcode] = 1', [':nid' => $node->id()])->fetchField();
  }

  /** * {@inheritdoc} */
  public function updateType($old_type$new_type) {
    return $this->database->update($this->getBaseTable())
      ->fields(['type' => $new_type])
      ->condition('type', $old_type)
      ->execute();
  }

  /** * {@inheritdoc} */
  public function clearRevisionsLanguage(LanguageInterface $language) {
    return $this->database->update($this->getRevisionTable())
      ->fields(['langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED])
      ->condition('langcode', $language->getId())
      
'd7_tracker_node',
    ]);
  }

  /** * Tests migration of tracker node table. */
  public function testMigrateTrackerNode() {
    $connection = Database::getConnection('default', 'migrate');
    $num_rows = $connection
      ->select('tracker_node', 'tn')
      ->fields('tn', ['nid', 'published', 'changed'])
      ->countQuery()
      ->execute()
      ->fetchField();
    $this->assertSame('1', $num_rows);

    $tracker_nodes = $connection
      ->select('tracker_node', 'tn')
      ->fields('tn', ['nid', 'published', 'changed'])
      ->execute();
    $row = $tracker_nodes->fetchAssoc();
    $this->assertSame('1', $row['nid']);
    

  public function fields() {
    return array_combine($this->variables, $this->variables);
  }

  /** * {@inheritdoc} */
  public function query() {
    return $this->getDatabase()
      ->select('i18n_variable', 'v')
      ->fields('v')
      ->condition('name', (array) $this->configuration['variables'], 'IN');
  }

  /** * {@inheritdoc} */
  public function getIds() {
    $ids['language']['type'] = 'string';
    return $ids;
  }

  

class UserEntityTranslation extends FieldableEntity {

  /** * {@inheritdoc} */
  public function query() {
    $query = $this->select('entity_translation', 'et')
      ->fields('et')
      ->condition('et.entity_type', 'user')
      ->condition('et.source', '', '<>');

    return $query;
  }

  /** * {@inheritdoc} */
  public function prepareRow(Row $row) {
    $uid = $row->getSourceProperty('entity_id');
    

class UpdateTest extends DatabaseTestBase {

  /** * Confirms that we can update a single record successfully. */
  public function testSimpleUpdate() {
    $num_updated = $this->connection->update('test')
      ->fields(['name' => 'Tiffany'])
      ->condition('id', 1)
      ->execute();
    $this->assertSame(1, $num_updated, 'Updated 1 record.');

    $saved_name = $this->connection->query('SELECT [name] FROM {test} WHERE [id] = :id', [':id' => 1])->fetchField();
    $this->assertSame('Tiffany', $saved_name, 'Updated name successfully.');
  }

  /** * Confirms updating to NULL. */
  
// No access for all languages as both node access modules deny access.     $this->assertNodeAccess($expected_node_access_no_access$this->nodes['private_no_language_private']$this->webUser);

    // No access for all languages as the non language aware node access module     // denies access.     $this->assertNodeAccess($expected_node_access_no_access$this->nodes['private_no_language_public']$this->webUser);

    // Query the node table with the node access tag in several languages.     $connection = Database::getConnection();
    // Query with no language specified. The fallback (hu or und) will be used.     $select = $connection->select('node', 'n')
      ->fields('n', ['nid'])
      ->addMetaData('account', $this->webUser)
      ->addTag('node_access');
    $nids = $select->execute()->fetchAllAssoc('nid');

    // Four nodes should be returned with public Hungarian translations or the     // no language public node.     $this->assertCount(4, $nids, 'Query returns 4 nodes when no langcode is specified.');
    $this->assertArrayHasKey($this->nodes['public_both_public']->id()$nids);
    $this->assertArrayHasKey($this->nodes['public_ca_private']->id()$nids);
    $this->assertArrayHasKey($this->nodes['private_both_public']->id()$nids);
    $this->assertArrayHasKey($this->nodes['public_no_language_public']->id()$nids);

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