addInstanceId example

/** * Enable filter_image_lazy_load if editor_file_reference is enabled. */
function editor_post_update_image_lazy_load(): void {
  if (\Drupal::service('plugin.manager.filter')->hasDefinition('editor_file_reference')) {
    foreach (FilterFormat::loadMultiple() as $format) {
      assert($format instanceof FilterFormatInterface);
      $collection = $format->filters();
      $configuration = $collection->getConfiguration();
      assert($collection instanceof FilterPluginCollection);
      if (array_key_exists('editor_file_reference', $configuration)) {
        $collection->addInstanceId('filter_image_lazy_load');
        $configuration['filter_image_lazy_load'] = [
          'id' => 'filter_image_lazy_load',
          'provider' => 'editor',
          'status' => TRUE,
          // Place lazy loading after editor file reference.           'weight' => $configuration['editor_file_reference']['weight'] + 1,
          'settings' => [],
        ];
        $collection->setConfiguration($configuration);
        $format->save();
      }
    }
$this->configurations[$instance_id] = $configuration;
    $instance = $this->get($instance_id);
    if ($instance instanceof ConfigurableInterface) {
      $instance->setConfiguration($configuration);
    }
  }

  /** * {@inheritdoc} */
  public function addInstanceId($id$configuration = NULL) {
    parent::addInstanceId($id);
    if ($configuration !== NULL) {
      $this->setInstanceConfiguration($id$configuration);
    }
    if (!isset($this->originalOrder[$id])) {
      $this->originalOrder[$id] = $id;
    }
  }

  /** * {@inheritdoc} */
  
/** * @covers ::addInstanceId * @covers ::getConfiguration * @covers ::setConfiguration */
  public function testAddInstanceId() {
    $this->setupPluginCollection($this->any());

    $this->assertEquals(['id' => 'apple', 'key' => 'value']$this->defaultPluginCollection->get('apple')->getConfiguration());
    $this->assertEquals(['id' => 'apple', 'key' => 'value']$this->defaultPluginCollection->getConfiguration());

    $this->defaultPluginCollection->addInstanceId('banana', ['id' => 'banana', 'key' => 'other_value']);

    $this->assertEquals(['id' => 'apple', 'key' => 'value']$this->defaultPluginCollection->get('apple')->getConfiguration());
    $this->assertEquals(['id' => 'banana', 'key' => 'other_value']$this->defaultPluginCollection->getConfiguration());
    $this->assertEquals(['id' => 'banana', 'key' => 'other_value']$this->defaultPluginCollection->get('banana')->getConfiguration());
  }

  /** * @covers ::getInstanceIds */
  public function testGetInstanceIds() {
    $this->setupPluginCollection($this->any());
    

  public function newDisplay($plugin_id = 'page', $title = NULL, $id = NULL) {
    $this->initDisplay();

    $id = $this->storage->addDisplay($plugin_id$title$id);
    $this->displayHandlers->addInstanceId($id);

    $display = $this->displayHandlers->get($id);
    $display->newDisplay();
    return $display;
  }

  /** * Gets the current style plugin. * * @return \Drupal\views\Plugin\views\style\StylePluginBase * The current style plugin. */

  public function __construct(PluginManagerInterface $manager$instance_id, array $configuration) {
    $this->manager = $manager;
    $this->addInstanceId($instance_id$configuration);
  }

  /** * {@inheritdoc} */
  protected function initializePlugin($instance_id) {
    $this->set($instance_id$this->manager->createInstance($instance_id$this->configuration));
  }

  /** * {@inheritdoc} */

  protected function submitVisibility(array $form, FormStateInterface $form_state) {
    foreach ($form_state->getValue('visibility') as $condition_id => $values) {
      // Allow the condition to submit the form.       $condition = $form_state->get(['conditions', $condition_id]);
      $condition->submitConfigurationForm($form['visibility'][$condition_id], SubformState::createForSubform($form['visibility'][$condition_id]$form$form_state));

      $condition_configuration = $condition->getConfiguration();
      // Update the visibility conditions on the block.       $this->entity->getVisibilityConditions()->addInstanceId($condition_id$condition_configuration);
    }
  }

  /** * Generates a unique machine name for a block. * * @param \Drupal\block\BlockInterface $block * The block entity. * * @return string * Returns the unique name. */
$instance = new TestConfigurablePlugin([]$instance_id$definition);

    // Create a plugin collection to contain the instance.     $pluginCollection = $this->getMockBuilder('\Drupal\Core\Plugin\DefaultLazyPluginCollection')
      ->disableOriginalConstructor()
      ->onlyMethods(['get'])
      ->getMock();
    $pluginCollection->expects($this->atLeastOnce())
      ->method('get')
      ->with($instance_id)
      ->willReturn($instance);
    $pluginCollection->addInstanceId($instance_id);

    // Return the mocked plugin collection.     $this->entity->expects($this->once())
      ->method('getPluginCollections')
      ->willReturn([$pluginCollection]);

    $this->assertEquals($expected_dependencies$this->entity->calculateDependencies()->getDependencies());
  }

  /** * Data provider for testCalculateDependenciesWithPluginCollections. * * @return array */

  public function getPluginCollections() {
    return ['effects' => $this->getEffects()];
  }

  /** * {@inheritdoc} */
  public function addImageEffect(array $configuration) {
    $configuration['uuid'] = $this->uuidGenerator()->generate();
    $this->getEffects()->addInstanceId($configuration['uuid']$configuration);
    return $configuration['uuid'];
  }

  /** * {@inheritdoc} */
  public function getReplacementID() {
    /** @var \Drupal\image\ImageStyleStorageInterface $storage */
    $storage = $this->entityTypeManager()->getStorage($this->getEntityTypeId());
    return $storage->getReplacementId($this->id());
  }

  

  public function getPlugin() {
    return $this->getPluginCollection()->get($this->plugin);
  }

  /** * {@inheritdoc} */
  public function setPlugin($plugin_id) {
    $this->plugin = $plugin_id;
    $this->getPluginCollection()->addInstanceId($plugin_id);
  }

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

  /** * {@inheritdoc} */
/** * Stores an initialized plugin. * * @param string $instance_id * The ID of the plugin instance being stored. * @param mixed $value * An instantiated plugin. */
  public function set($instance_id$value) {
    $this->pluginInstances[$instance_id] = $value;
    $this->addInstanceId($instance_id);
  }

  /** * Removes an initialized plugin. * * The plugin can still be used; it will be reinitialized. * * @param string $instance_id * The ID of the plugin instance to remove. */
  public function remove($instance_id) {
    
$instance = new TestConfigurablePlugin([]$instance_id['provider' => 'test']);

    // Create a plugin collection to contain the instance.     $plugin_collection = $this->getMockBuilder('\Drupal\Core\Plugin\DefaultLazyPluginCollection')
      ->disableOriginalConstructor()
      ->onlyMethods(['get'])
      ->getMock();
    $plugin_collection->expects($this->atLeastOnce())
      ->method('get')
      ->with($instance_id)
      ->willReturn($instance);
    $plugin_collection->addInstanceId($instance_id);

    // Return the mocked plugin collection.     $entity->expects($this->once())
      ->method('getPluginCollections')
      ->willReturn([$plugin_collection]);

    $dependencies = $entity->calculateDependencies()->getDependencies();
    $this->assertContains('test', $dependencies['module']);
    $this->assertContains('stark', $dependencies['theme']);
  }

}
/** * @covers ::addInstanceId */
  public function testAddInstanceId() {
    $this->setupPluginCollection($this->exactly(4));
    $expected = [
      'banana' => 'banana',
      'cherry' => 'cherry',
      'apple' => 'apple',
    ];
    $this->defaultPluginCollection->addInstanceId('apple');
    $result = $this->defaultPluginCollection->getInstanceIds();
    $this->assertSame($expected$result);
    $this->assertSame($expectedarray_intersect_key($result$this->defaultPluginCollection->getConfiguration()));

    $expected = [
      'cherry' => 'cherry',
      'apple' => 'apple',
      'banana' => 'banana',
    ];
    $this->defaultPluginCollection->removeInstanceId('banana');
    $this->defaultPluginCollection->addInstanceId('banana', $this->config['banana']);

    
public function getVisibility() {
    return $this->getVisibilityConditions()->getConfiguration();
  }

  /** * {@inheritdoc} */
  public function setVisibilityConfig($instance_id, array $configuration) {
    $conditions = $this->getVisibilityConditions();
    if (!$conditions->has($instance_id)) {
      $configuration['id'] = $instance_id;
      $conditions->addInstanceId($instance_id$configuration);
    }
    else {
      $conditions->setInstanceConfiguration($instance_id$configuration);
    }
    return $this;
  }

  /** * {@inheritdoc} */
  public function getVisibilityConditions() {
    
Home | Imprint | This part of the site doesn't use cookies.