isTranslatable example

/** * Tests that field synchronization is skipped for disabled bundles. */
  public function testFieldSynchronizationWithDisabledBundle() {
    $entity = EntityTestMul::create();
    $entity->save();

    /** @var \Drupal\Core\Entity\ContentEntityInterface $translation */
    $translation = $entity->addTranslation('it');
    $translation->save();

    $this->assertTrue($entity->isTranslatable());
  }

  /** * Tests that bundle translation settings are propagated on creation. * * @throws \Drupal\Core\Entity\EntityStorageException */
  public function testBundleClearOnLanguageContentSettingInsert() {
    $node = $this->getBundledNode();
    $this->assertFalse($node->isTranslatable());
    $this->contentTranslationManager->setEnabled('node', 'bundle_test', TRUE);
    
'title' => $this->t('Reset'),
        'url' => $this->getResetRoute(),
      ];
    }
    elseif ($this->isDeletable()) {
      $operations['delete'] = [
        'title' => $this->t('Delete'),
        'url' => $this->getDeleteRoute(),
      ];
    }

    if ($this->isTranslatable()) {
      $operations['translate'] = [
        'title' => $this->t('Translate'),
        'url' => $this->getTranslateRoute(),
      ];
    }

    return $operations;
  }

  /** * {@inheritdoc} */

  protected function assertIds(MigrateSourceInterface $source, array $configuration): void {
    $ids = $source->getIds();
    [$entity_type_id] = explode(PluginBase::DERIVATIVE_SEPARATOR, $source->getPluginId());
    $entity_type = \Drupal::entityTypeManager()->getDefinition($entity_type_id);

    $this->assertArrayHasKey($entity_type->getKey('id')$ids);
    $ids_count_expected = 1;

    if ($entity_type->isTranslatable()) {
      $ids_count_expected++;
      $this->assertArrayHasKey($entity_type->getKey('langcode')$ids);
    }

    if ($entity_type->isRevisionable() && $configuration['add_revision_id']) {
      $ids_count_expected++;
      $this->assertArrayHasKey($entity_type->getKey('revision')$ids);
    }

    $this->assertCount($ids_count_expected$ids);
  }

  
/** * {@inheritdoc} */
  public function isDeletable() {
    return TRUE;
  }

  /** * {@inheritdoc} */
  public function isTranslatable() {
    return $this->getEntity()->isTranslatable();
  }

  /** * {@inheritdoc} */
  public function deleteLink() {
    $this->getEntity()->delete();
  }

}


  /** * {@inheritdoc} */
  public function validate($entity, Constraint $constraint) {
    /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
    /** @var \Drupal\Core\Entity\Plugin\Validation\Constraint\EntityUntranslatableFieldsConstraint $constraint */

    // Untranslatable field restrictions apply only to revisions of multilingual     // entities.     if ($entity->isNew() || !$entity->isTranslatable() || !$entity->getEntityType()->isRevisionable()) {
      return;
    }
    if ($entity->isDefaultRevision() && !$entity->isDefaultTranslationAffectedOnly()) {
      return;
    }

    // To avoid unintentional reverts and data losses, we forbid changes to     // untranslatable fields in pending revisions for multilingual entities. The     // only case where changes in pending revisions are acceptable is when     // untranslatable fields affect only the default translation, in which case     // a pending revision contains only one affected translation. Even in this

      }

      // If we have a list of moderated bundles, restrict the query to show only       // entities in those bundles.       if ($moderated_bundles) {
        $entity_base_table_alias = $this->relationship ?: $this->table;

        // The bundle field of an entity type is not revisionable so we need to         // join the base table.         $entity_base_table = $entity_type->getBaseTable();
        $entity_revision_base_table = $entity_type->isTranslatable() ? $entity_type->getRevisionDataTable() : $entity_type->getRevisionTable();
        if ($this->table === $entity_revision_base_table) {
          $configuration = [
            'table' => $entity_base_table,
            'field' => $entity_type->getKey('id'),
            'left_table' => $entity_revision_base_table,
            'left_field' => $entity_type->getKey('id'),
            'type' => 'INNER',
          ];

          $join = Views::pluginManager('join')->createInstance('standard', $configuration);
          $entity_base_table_alias = $this->query->addRelationship($entity_base_table$join$entity_revision_base_table);
        }

  public function testEntityBaseFieldInfo() {
    $definition = $this->entityTypeManager->getDefinition('entity_test');
    $definition->setHandlerClass('moderation', ModerationHandler::class);

    $this->enableModeration('entity_test', 'entity_test');
    $base_fields = $this->entityTypeInfo->entityBaseFieldInfo($definition);

    $this->assertFalse($base_fields['moderation_state']->isReadOnly());
    $this->assertTrue($base_fields['moderation_state']->isComputed());
    $this->assertTrue($base_fields['moderation_state']->isTranslatable());
  }

  /** * Tests the correct entity types have moderation added. * * @covers ::entityTypeAlter * * @dataProvider providerTestEntityTypeAlter */
  public function testEntityTypeAlter($entity_type_id$moderatable) {
    $entity_types = $this->entityTypeManager->getDefinitions();
    

    $this->drupalGet('admin/config/regional/content-language');
    $this->submitForm($edit, 'Save configuration');
  }

  /** * Tests private file fields on translated nodes. */
  public function testPrivateLanguageFile() {
    // Verify that the file field on the "Basic page" node type is translatable.     $definitions = \Drupal::service('entity_field.manager')->getFieldDefinitions('node', 'page');
    $this->assertTrue($definitions[$this->fieldName]->isTranslatable(), 'Node file field is translatable.');

    // Create a default language node.     $default_language_node = $this->drupalCreateNode(['type' => 'page']);

    // Edit the node to upload a file.     $edit = [];
    $name = 'files[' . $this->fieldName . '_0]';
    $edit[$name] = \Drupal::service('file_system')->realpath($this->drupalGetTestFiles('text')[0]->uri);
    $this->drupalGet('node/' . $default_language_node->id() . '/edit');
    $this->submitForm($edit, 'Save');
    $last_fid_prior = $this->getLastFileId();

    

  protected function checkFieldStorageDefinitionTranslatability($field_name) {
    return array_key_exists($field_name$this->fieldStorageDefinitions) && $this->fieldStorageDefinitions[$field_name]->isTranslatable();
  }

  /** * {@inheritdoc} */
  public function retranslate(EntityInterface $entity$langcode = NULL) {
    $updated_langcode = !empty($langcode) ? $langcode : $entity->language()->getId();
    foreach ($entity->getTranslationLanguages() as $langcode => $language) {
      $this->manager->getTranslationMetadata($entity->getTranslation($langcode))
        ->setOutdated($langcode != $updated_langcode);
    }
  }
->save();
    }
  }

  /** * Tests translatable fields storage/retrieval. */
  public function testTranslatableFieldSaveLoad() {
    // Enable field translations for nodes.     field_test_entity_info_translatable('node', TRUE);
    $entity_type = \Drupal::entityTypeManager()->getDefinition('node');
    $this->assertTrue($entity_type->isTranslatable(), 'Nodes are translatable.');

    // Prepare the field translations.     $entity_type_id = 'entity_test';
    field_test_entity_info_translatable($entity_type_id, TRUE);
    $entity = $this->container->get('entity_type.manager')
      ->getStorage($entity_type_id)
      ->create(['type' => $this->field->getTargetBundle()]);
    $field_translations = [];
    $available_langcodes = array_keys($this->container->get('language_manager')->getLanguages());
    $entity->langcode->value = reset($available_langcodes);
    foreach ($available_langcodes as $langcode) {
      
    return $this->listLanguages(LanguageInterface::STATE_CONFIGURABLE | LanguageInterface::STATE_SITE_DEFAULT | PluginBase::INCLUDE_NEGOTIATED | PluginBase::INCLUDE_ENTITY, [$this->getOption('rendering_language')]);
  }

  /** * Returns whether the base table is of a translatable entity type. * * @return bool * TRUE if the base table is of a translatable entity type, FALSE otherwise. */
  protected function isBaseTableTranslatable() {
    if ($entity_type = $this->view->getBaseEntityType()) {
      return $entity_type->isTranslatable();
    }
    return FALSE;
  }

}

/** * @} */

  public function testVocabularyFieldInstance() {
    $this->executeMigration('d6_vocabulary_field_instance');

    // Test that the field exists. Tags has a multilingual option of 'None'.     $field_id = 'node.article.field_tags';
    $field = FieldConfig::load($field_id);
    $this->assertSame($field_id$field->id(), 'Field instance exists on article bundle.');
    $this->assertSame('Tags', $field->label());
    $this->assertTrue($field->isRequired(), 'Field is required');
    $this->assertFalse($field->isTranslatable());
    $this->assertTargetBundles($field_id['tags' => 'tags']);

    // Test the page bundle as well. Tags has a multilingual option of 'None'.     $field_id = 'node.page.field_tags';
    $field = FieldConfig::load($field_id);
    $this->assertSame($field_id$field->id(), 'Field instance exists on page bundle.');
    $this->assertSame('Tags', $field->label());
    $this->assertTrue($field->isRequired(), 'Field is required');
    $this->assertFalse($field->isTranslatable());

    $settings = $field->getSettings();
    
if ($entity_type_definition->hasKey('bundle')) {
            $derivative['source']['bundle'] = $bundle;
          }

          // Set the process pipeline.           $id_key = $entity_type_definition->getKey('id');
          $derivative['process'][$id_key] = $id_key;
          if ($entity_type_definition->isRevisionable()) {
            $revision_key = $entity_type_definition->getKey('revision');
            $derivative['process'][$revision_key] = $revision_key;
          }
          if ($entity_type_definition->isTranslatable()) {
            $langcode_key = $entity_type_definition->getKey('langcode');
            $derivative['process'][$langcode_key] = $langcode_key;
          }

          // Set the destination plugin.           $derivative['destination']['plugin'] = 'entity' . PluginBase::DERIVATIVE_SEPARATOR . $entity_type;
          if ($entity_type_definition->hasKey('bundle')) {
            $derivative['destination']['default_bundle'] = $bundle;
          }
          if ($entity_type_definition->isTranslatable()) {
            $derivative['destination']['translations'] = TRUE;
          }


  /** * {@inheritdoc} */
  public function viewField(FieldItemListInterface $items$display_options = []) {
    /** @var \Drupal\Core\Entity\FieldableEntityInterface $entity */
    $entity = $items->getEntity();
    // If the field is not translatable and the entity is, then the field item     // list always points to the default translation of the entity. Attempt to     // fetch it in the current content language.     if (!$items->getFieldDefinition()->isTranslatable() && $entity->isTranslatable()) {
      $entity = $this->entityRepository->getTranslationFromContext($entity);
    }

    $field_name = $items->getFieldDefinition()->getName();
    $display = $this->getSingleFieldDisplay($entity$field_name$display_options);

    $output = [];
    $build = $display->build($entity);
    if (isset($build[$field_name])) {
      $output = $build[$field_name];
    }

    
return static::create($definition->getType())
      ->setCardinality($definition->getCardinality())
      ->setConstraints($definition->getConstraints())
      ->setCustomStorage($definition->hasCustomStorage())
      ->setDescription($definition->getDescription())
      ->setLabel($definition->getLabel())
      ->setName($definition->getName())
      ->setProvider($definition->getProvider())
      ->setRevisionable($definition->isRevisionable())
      ->setSettings($definition->getSettings())
      ->setTargetEntityTypeId($definition->getTargetEntityTypeId())
      ->setTranslatable($definition->isTranslatable());
  }

  /** * {@inheritdoc} */
  public static function createFromItemType($item_type) {
    // The data type of a field item is in the form of "field_item:$field_type".     $parts = explode(':', $item_type, 2);
    return static::create($parts[1]);
  }

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