setFieldMap example

// Set an arbitrary metadata value to be mapped.     $this->container->get('state')
      ->set('media_source_test_attributes', [
        'attribute_to_map' => [
          'title' => 'Attribute to map',
          'value' => 'Snowball',
        ],
        'thumbnail_uri' => [
          'value' => 'public://TheSisko.png',
        ],
      ]);
    $this->testMediaType->setFieldMap([
      'attribute_to_map' => 'field_to_map_to',
    ])->save();

    /** @var \Drupal\Core\Entity\EntityStorageInterface $storage */
    $storage = $this->container->get('entity_type.manager')
      ->getStorage('media');

    /** @var \Drupal\media\MediaInterface $a */
    $a = $storage->create([
      'bundle' => $this->testMediaType->id(),
    ]);
    
    // do not have to rebuild it again.     if ($field_map = $this->entityFieldManager->getFieldMap()) {
      if (!isset($field_map[$entity_type_id][$field_name])) {
        // This field did not exist yet, initialize it with the type and empty         // bundle list.         $field_map[$entity_type_id][$field_name] = [
          'type' => $field_definition->getType(),
          'bundles' => [],
        ];
      }
      $field_map[$entity_type_id][$field_name]['bundles'][$bundle] = $bundle;
      $this->entityFieldManager->setFieldMap($field_map);
    }
  }

  /** * {@inheritdoc} */
  public function onFieldDefinitionUpdate(FieldDefinitionInterface $field_definition, FieldDefinitionInterface $original) {
    // Notify the storage about the updated field.     $this->entityTypeManager->getStorage($field_definition->getTargetEntityTypeId())->onFieldDefinitionUpdate($field_definition$original);
  }

  
protected $defaultTheme = 'stark';

  /** * Test that non-main properties do not trigger source field value change. */
  public function testOnlyMainPropertiesTriggerSourceFieldChanged() {
    $assert_session = $this->assertSession();
    $page = $this->getSession()->getPage();

    $media_type = $this->createMediaType('image');
    $media_type_id = $media_type->id();
    $media_type->setFieldMap(['name' => 'name']);
    $media_type->save();

    /** @var \Drupal\field\FieldConfigInterface $field */
    // Disable the alt text field, because this is not a JavaScript test and     // the alt text field will therefore not appear without a full page refresh.     $field = FieldConfig::load("media.$media_type_id.field_media_image");
    $settings = $field->getSettings();
    $settings['alt_field'] = TRUE;
    $settings['alt_field_required'] = FALSE;
    $field->set('settings', $settings);
    $field->save();

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