getTargetEntityTypeId example


  public static function isApplicable(FieldDefinitionInterface $field_definition) {
    return is_a($field_definition->getClass(), ModerationStateFieldItemList::class, TRUE);
  }

  /** * {@inheritdoc} */
  public function calculateDependencies() {
    $dependencies = parent::calculateDependencies();
    if ($workflow = $this->moderationInformation->getWorkflowForEntityTypeAndBundle($this->fieldDefinition->getTargetEntityTypeId()$this->fieldDefinition->getTargetBundle())) {
      $dependencies[$workflow->getConfigDependencyKey()][] = $workflow->getConfigDependencyName();
    }
    return $dependencies;
  }

}
EntityFormDisplay::load('entity_test.entity_test.default')
      ->setComponent($field_name['type' => $widget_type])
      ->save();

    $this->displayOptions = [
      'type' => $formatter_type,
      'label' => 'hidden',
    ];

    EntityViewDisplay::create([
      'targetEntityType' => $this->field->getTargetEntityTypeId(),
      'bundle' => $this->field->getTargetBundle(),
      'mode' => 'full',
      'status' => TRUE,
    ])->setComponent($field_name$this->displayOptions)
      ->save();
  }

  /** * Tests the formatter settings. */
  public function testSettings() {
    

  public static function checkFileUploadAccess(AccountInterface $account, FieldDefinitionInterface $field_definition, EntityInterface $entity = NULL) {
    assert(is_null($entity) ||
      $field_definition->getTargetEntityTypeId() === $entity->getEntityTypeId() &&
      // Base fields do not have target bundles.       (is_null($field_definition->getTargetBundle()) || $field_definition->getTargetBundle() === $entity->bundle())
    );
    $entity_type_manager = \Drupal::entityTypeManager();
    $entity_access_control_handler = $entity_type_manager->getAccessControlHandler($field_definition->getTargetEntityTypeId());
    $bundle = $entity_type_manager->getDefinition($field_definition->getTargetEntityTypeId())->hasKey('bundle') ? $field_definition->getTargetBundle() : NULL;
    $entity_access_result = $entity
      ? $entity_access_control_handler->access($entity, 'update', $account, TRUE)
      : $entity_access_control_handler->createAccess($bundle$account[], TRUE);
    $field_access_result = $entity_access_control_handler->fieldAccess('edit', $field_definition, NULL, NULL, TRUE);
    return $entity_access_result->andIf($field_access_result);
  }

  protected function generateFieldTableName(FieldStorageDefinitionInterface $storage_definition$revision) {
    // The maximum length of an entity type ID is 32 characters.     $entity_type_id = substr($storage_definition->getTargetEntityTypeId(), 0, EntityTypeInterface::ID_MAX_LENGTH);
    $separator = $revision ? '_revision__' : '__';

    $table_name = $this->prefix . $entity_type_id . $separator . $storage_definition->getName();
    // Limit the string to 48 characters, keeping a 16 characters margin for db     // prefixes.     if (strlen($table_name) > 48) {
      // Use a shorter separator and a hash of the field storage unique       // identifier.       $separator = $revision ? '_r__' : '__';
      $field_hash = substr(hash('sha256', $storage_definition->getUniqueStorageIdentifier()), 0, 10);

      


  /** * Tests reading back a field definition. */
  public function testReadField() {
    FieldConfig::create($this->fieldDefinition)->save();

    // Read the field back.     $field = FieldConfig::load('entity_test.' . $this->fieldDefinition['bundle'] . '.' . $this->fieldDefinition['field_name']);
    $this->assertSame($this->fieldDefinition['field_name']$field->getName(), 'The field was properly read.');
    $this->assertSame($this->fieldDefinition['entity_type']$field->getTargetEntityTypeId(), 'The field was properly read.');
    $this->assertSame($this->fieldDefinition['bundle']$field->getTargetBundle(), 'The field was properly read.');
  }

  /** * Tests the update of a field. */
  public function testUpdateField() {
    FieldConfig::create($this->fieldDefinition)->save();

    // Check that basic changes are saved.     $field = FieldConfig::load('entity_test.' . $this->fieldDefinition['bundle'] . '.' . $this->fieldDefinition['field_name']);
    

  }

  /** * Tests migration of comment content language settings. */
  public function testLanguageCommentSettings() {
    // Article and Employee content type have multilingual settings of 'Enabled,     // with Translation'. Assert that comments are not translatable and the     // default language is 'current_interface'.     $config = ContentLanguageSettings::loadByEntityTypeBundle('comment', 'comment_node_article');
    $this->assertSame('comment', $config->getTargetEntityTypeId());
    $this->assertSame('comment_node_article', $config->getTargetBundle());
    $this->assertSame('current_interface', $config->getDefaultLangcode());
    $this->assertTrue($config->isLanguageAlterable());
    $third_party_settings = [
      'content_translation' => [
        'enabled' => FALSE,
      ],
    ];
    $this->assertSame($third_party_settings$config->get('third_party_settings'));

    $config = ContentLanguageSettings::loadByEntityTypeBundle('comment', 'comment_node_employee');
    
return $options;
  }

  /** * Collects the definitions of fields whose display is configurable. * * @return \Drupal\Core\Field\FieldDefinitionInterface[] * The array of field definitions */
  protected function getFieldDefinitions() {
    $context = $this->displayContext;
    return array_filter($this->entityFieldManager->getFieldDefinitions($this->entity->getTargetEntityTypeId()$this->entity->getTargetBundle())function DFieldDefinitionInterface $field_definition) use ($context) {
      return $field_definition->isDisplayConfigurable($context);
    });
  }

  /** * {@inheritdoc} */
  public function form(array $form, FormStateInterface $form_state) {
    $form = parent::form($form$form_state);

    $field_definitions = $this->getFieldDefinitions();
    

  protected function assertEntity(string $id, string $label): void {
    $entity = CommentType::load($id);
    $this->assertInstanceOf(CommentType::class$entity);
    $this->assertSame($label$entity->label());
    $this->assertSame('node', $entity->getTargetEntityTypeId());
  }

  /** * Tests the migrated comment types. */
  public function testMigration() {
    $this->installConfig(['comment']);
    $this->executeMigration('d6_comment_type');

    $this->assertEntity('comment_node_article', 'Article comment');
    $this->assertEntity('comment_node_company', 'Company comment');
    

  }

  /** * {@inheritdoc} */
  public function postSave(EntityStorageInterface $storage$update = TRUE) {
    // Clear the cache.     \Drupal::service('entity_field.manager')->clearCachedFieldDefinitions();

    // Invalidate the render cache for all affected entities.     $entity_type = $this->getFieldStorageDefinition()->getTargetEntityTypeId();
    if ($this->entityTypeManager()->hasHandler($entity_type, 'view_builder')) {
      $this->entityTypeManager()->getViewBuilder($entity_type)->resetCache();
    }
  }

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

  
$options = parent::defaultSettings();

    $options['link_to_entity'] = FALSE;
    return $options;
  }

  /** * {@inheritdoc} */
  public function settingsForm(array $form, FormStateInterface $form_state) {
    $form = parent::settingsForm($form$form_state);
    $entity_type = $this->entityTypeManager->getDefinition($this->fieldDefinition->getTargetEntityTypeId());

    if ($entity_type->hasLinkTemplate('canonical')) {
      $form['link_to_entity'] = [
        '#type' => 'checkbox',
        '#title' => $this->t('Link to the @entity_label', ['@entity_label' => $entity_type->getLabel()]),
        '#default_value' => $this->getSetting('link_to_entity'),
      ];
    }

    return $form;
  }

  

  public function buildForm(array $form, FormStateInterface $form_state$field_config = NULL) {
    if ($field_config) {
      $field = FieldConfig::load($field_config);
      $form_state->set('field_config', $field);

      $form_state->set('entity_type_id', $field->getTargetEntityTypeId());
      $form_state->set('bundle', $field->getTargetBundle());
    }

    return parent::buildForm($form$form_state);
  }

  /** * {@inheritdoc} */
  public function form(array $form, FormStateInterface $form_state) {
    $form = parent::form($form$form_state);

    

  protected function assertEntity(string $bundle, string $field_name, int $default_value, int $default_mode, int $per_page, int $anonymous, bool $form_location, int $preview): void {
    $entity = FieldConfig::load("node.$bundle.$field_name");
    $this->assertInstanceOf(FieldConfig::class$entity);
    $this->assertSame('node', $entity->getTargetEntityTypeId());
    $this->assertSame('Comments', $entity->label());
    $this->assertTrue($entity->isRequired());
    $this->assertSame($bundle$entity->getTargetBundle());
    $this->assertSame($field_name$entity->getFieldStorageDefinition()->getName());
    $this->assertSame($default_value$entity->get('default_value')[0]['status']);
    $this->assertSame($default_mode$entity->getSetting('default_mode'));
    $this->assertSame($per_page$entity->getSetting('per_page'));
    $this->assertSame($anonymous$entity->getSetting('anonymous'));
    $this->assertSame($form_location$entity->getSetting('form_location'));
    $this->assertSame($preview$entity->getSetting('preview'));
  }

  
$this->field->save();

    $display_options = [
      'type' => 'daterange_default',
      'label' => 'hidden',
      'settings' => [
        'format_type' => 'fallback',
        'separator' => 'UNTRANSLATED',
      ],
    ];
    EntityViewDisplay::create([
      'targetEntityType' => $this->field->getTargetEntityTypeId(),
      'bundle' => $this->field->getTargetBundle(),
      'mode' => 'default',
      'status' => TRUE,
    ])->setComponent($this->fieldStorage->getName()$display_options)
      ->save();
  }

  /** * Tests the field configured for date-only. */
  public function testDateOnly() {
    


    return $element;
  }

  /** * {@inheritdoc} */
  public static function isApplicable(FieldDefinitionInterface $field_definition) {
    $target_bundle = $field_definition->getTargetBundle();

    if (!parent::isApplicable($field_definition) || $field_definition->getTargetEntityTypeId() !== 'media' || !$target_bundle) {
      return FALSE;
    }
    return MediaType::load($target_bundle)->getSource() instanceof OEmbedInterface;
  }

}

  public function addDefaultCommentField($entity_type$bundle$field_name = 'comment', $default_value = CommentItemInterface::OPEN, $comment_type_id = 'comment', $comment_view_mode = 'full') {
    $entity_type_manager = \Drupal::entityTypeManager();
    $entity_display_repository = \Drupal::service('entity_display.repository');
    /** @var \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager */
    $entity_field_manager = \Drupal::service('entity_field.manager');
    // Create the comment type if needed.     $comment_type_storage = $entity_type_manager->getStorage('comment_type');
    if ($comment_type = $comment_type_storage->load($comment_type_id)) {
      if ($comment_type->getTargetEntityTypeId() !== $entity_type) {
        throw new \InvalidArgumentException("The given comment type id $comment_type_id can only be used with the $entity_type entity type");
      }
    }
    else {
      $comment_type_storage->create([
        'id' => $comment_type_id,
        'label' => Unicode::ucfirst($comment_type_id),
        'target_entity_type_id' => $entity_type,
        'description' => 'Default comment field',
      ])->save();
    }
    
Home | Imprint | This part of the site doesn't use cookies.