createInstance example

$block->getPluginId()->willReturn('block_plugin_id');
    $block->getBaseId()->willReturn('block_plugin_id');
    $block->getDerivativeId()->willReturn(NULL);
    $placeholder_label = 'Placeholder Label';
    $block->getPreviewFallbackString()->willReturn($placeholder_label);

    $block_content = [
      '#markup' => 'The block content.',
      '#cache' => ['tags' => ['build-tag']],
    ];
    $block->build()->willReturn($block_content);
    $this->blockManager->createInstance('some_block_id', ['id' => 'some_block_id'])->willReturn($block->reveal());

    $component = new SectionComponent('some-uuid', 'some-region', ['id' => 'some_block_id']);
    $in_preview = FALSE;
    $event = new SectionComponentBuildRenderArrayEvent($component$contexts$in_preview);

    $subscriber = new BlockComponentRenderArray($this->account->reveal());

    $expected_build = [
      '#theme' => 'block',
      '#weight' => 0,
      '#configuration' => [],
      
/** * {@inheritdoc} */
  protected static $modules = ['node', 'user'];

  /** * Tests that DefaultFactory can create a plugin instance. */
  public function testDefaultFactory() {
    // Ensure a non-derivative plugin can be instantiated.     $plugin = $this->testPluginManager->createInstance('user_login', ['title' => 'Please enter your login name and password']);
    $this->assertSame('Drupal\\plugin_test\\Plugin\\plugin_test\\mock_block\\MockUserLoginBlock', get_class($plugin), 'Correct plugin class instantiated with default factory.');
    $this->assertSame('Please enter your login name and password', $plugin->getTitle(), 'Plugin instance correctly configured.');

    // Ensure that attempting to instantiate non-existing plugins throws a     // PluginException.     try {
      $this->testPluginManager->createInstance('non_existing');
      $this->fail('Drupal\Component\Plugin\Exception\ExceptionInterface expected');
    }
    catch (\Exception $e) {
      $this->assertInstanceOf(ExceptionInterface::class$e);
    }

  public function build(SectionStorageInterface $section_storage, int $delta) {
    $items = [];
    $definitions = $this->layoutManager->getFilteredDefinitions('layout_builder', $this->getPopulatedContexts($section_storage)['section_storage' => $section_storage]);
    foreach ($definitions as $plugin_id => $definition) {
      $layout = $this->layoutManager->createInstance($plugin_id);
      $item = [
        '#type' => 'link',
        '#title' => [
          'icon' => $definition->getIcon(60, 80, 1, 3),
          'label' => [
            '#type' => 'container',
            '#children' => $definition->getLabel(),
          ],
        ],
        '#url' => Url::fromRoute(
          $layout instanceof PluginFormInterface ? 'layout_builder.configure_section' : 'layout_builder.add_section',
          [


  /** * {@inheritdoc} * * A specific createInstance method is necessary to pass the migration on. */
  public function createInstance($plugin_id, array $configuration = [], MigrationInterface $migration = NULL) {
    if (substr($plugin_id, 0, 7) == 'entity:' && !$this->entityTypeManager->getDefinition(substr($plugin_id, 7), FALSE)) {
      $plugin_id = 'null';
    }
    return parent::createInstance($plugin_id$configuration$migration);
  }

}

  protected function callMethodOnFieldPlugin(MigrateFieldPluginManagerInterface $field_plugin_manager$field_type$method, Row $row) {
    $plugin_id = $field_plugin_manager->getPluginIdFromFieldType($field_type[]$this->migration);
    $plugin_instance = $field_plugin_manager->createInstance($plugin_id[]$this->migration);
    if (!is_callable([$plugin_instance$method])) {
      throw new MigrateException('The specified method does not exist or is not callable.');
    }
    return call_user_func_array([$plugin_instance$method][$row]);
  }

}

  public function getFormId() {
    return 'condition_node_type_test_form';
  }

  /** * Constructs a \Drupal\condition_test\FormController object. */
  public function __construct() {
    $manager = new ConditionManager(\Drupal::service('container.namespaces'), \Drupal::cache('discovery'), \Drupal::moduleHandler());
    $this->condition = $manager->createInstance('entity_bundle:node');
    $this->conditionCurrentTheme = $manager->createInstance('current_theme');
  }

  /** * {@inheritdoc} */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form['#tree'] = TRUE;
    $form['entity_bundle'] = [];
    $subformState = SubformState::createForSubform($form['entity_bundle']$form$form_state);
    $form['entity_bundle'] = $this->condition->buildConfigurationForm($form['entity_bundle']$subformState);

    
// Create a user and a node, and update the search index.     $test_user = $this->drupalCreateUser([
      'access content',
      'search content',
      'administer nodes',
    ]);
    $this->drupalLogin($test_user);

    $this->node = $this->drupalCreateNode();

    $this->container->get('plugin.manager.search')->createInstance('node_search')->updateIndex();

    // Set up a dummy initial count of times the form has been submitted.     $this->submitCount = \Drupal::state()->get('search_embedded_form.submit_count');
    $this->refreshVariables();
  }

  /** * Tests that the embedded form appears and can be submitted. */
  public function testEmbeddedForm() {
    // First verify we can submit the form from the module's page.
// Create a plugin instance for each element of the hierarchy.       foreach ($base_routes as $base_route) {
        // Convert the tree keyed by plugin IDs into a simple one with         // integer depth. Create instances for each plugin along the way.         $level = 0;
        // We used this above as the top-level parent array key.         $next_parent = '> ' . $base_route;
        do {
          $parent = $next_parent;
          $next_parent = FALSE;
          foreach ($children[$parent] as $plugin_id => $task_info) {
            $plugin = $this->createInstance($plugin_id);
            $this->instances[$route_name][$level][$plugin_id] = $plugin;
            // Normally, the link generator compares the href of every link with             // the current path and sets the active class accordingly. But the             // parents of the current local task may be on a different route in             // which case we have to set the class manually by flagging it             // active.             if (!empty($parents[$plugin_id]) && $route_name != $task_info['route_name']) {
              $plugin->setActive();
            }
            if (isset($children[$plugin_id])) {
              // This tab has visible children.
/** * The entity bundle information service. * * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface */
  protected $bundleInfo;

  /** * {@inheritdoc} */
  public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
    $instance = parent::createInstance($container$entity_type);
    $instance->entityTypeManager = $container->get('entity_type.manager');
    $instance->workspaceAssociation = $container->get('workspaces.association');
    $instance->dateFormatter = $container->get('date.formatter');
    $instance->bundleInfo = $container->get('entity_type.bundle.info');
    return $instance;
  }

  /** * {@inheritdoc} */
  public function buildComponents(array &$build, array $entities, array $displays$view_mode) {
    

  protected function submitVisibility(array $form, FormStateInterface $form_state) {
    // Intentionally empty.   }

  /** * {@inheritdoc} */
  protected function getPluginForm(BlockPluginInterface $block) {
    if ($block instanceof PluginWithFormsInterface) {
      return $this->pluginFormFactory->createInstance($block, 'settings_tray', 'configure');
    }
    return $block;
  }

  /** * {@inheritdoc} */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildForm($form$form_state);
    $form['actions']['submit']['#ajax'] = [
      'callback' => '::ajaxSubmit',
    ];
/** * Tests the EntityExists plugin. */
  public function testEntityExists() {
    $user = User::create([
      'name' => $this->randomString(),
    ]);
    $user->save();
    $uid = $user->id();

    $plugin = \Drupal::service('plugin.manager.migrate.process')
      ->createInstance('entity_exists', [
        'entity_type' => 'user',
      ]);
    $executable = $this->prophesize(MigrateExecutableInterface::class)->reveal();
    $row = new Row();

    // Ensure that the entity ID is returned if it really exists.     $value = $plugin->transform($uid$executable$row, 'buffalo');
    $this->assertSame($uid$value);

    // Ensure that the plugin returns FALSE if the entity doesn't exist.     $value = $plugin->transform(420, $executable$row, 'buffalo');
    

    // Create nodes containing keywords.     for ($i = 0; $i <= 17; $i++) {
      $settings['body'] = [['value' => 'love cheesy pizza']];
      $this->drupalCreateNode($settings);
    }
    // Create another node and save it for later.     $settings['body'] = [['value' => 'Druplicon']];
    $node = $this->drupalCreateNode($settings);

    // Update the search index.     $this->container->get('plugin.manager.search')->createInstance('node_search')->updateIndex();

    // Refresh variables after the treatment.     $this->refreshVariables();

    // Test that the correct number of pager links are found for keyword search.     $edit = ['keys' => 'love pizza'];
    $this->drupalGet('search/node');
    $this->submitForm($edit, 'Search');
    $this->assertSession()->linkByHrefExists('page=1', 0, '2nd page link is found for keyword search.');
    $this->assertSession()->linkByHrefExists('page=2', 0, '3rd page link is found for keyword search.');
    $this->assertSession()->linkByHrefExists('page=3', 0, '4th page link is found for keyword search.');
    
$this->handlers[$handler_type][$entity_type_id] = $this->createHandlerInstance($class$definition);
    }

    return $this->handlers[$handler_type][$entity_type_id];
  }

  /** * {@inheritdoc} */
  public function createHandlerInstance($class, EntityTypeInterface $definition = NULL) {
    if (is_subclass_of($class, 'Drupal\Core\Entity\EntityHandlerInterface')) {
      $handler = $class::createInstance($this->container, $definition);
    }
    else {
      $handler = new $class($definition);
    }
    if (method_exists($handler, 'setModuleHandler')) {
      $handler->setModuleHandler($this->moduleHandler);
    }
    if (method_exists($handler, 'setStringTranslation')) {
      $handler->setStringTranslation($this->stringTranslation);
    }

    

  public function testFilterOptionAsMarkup() {
    $view = $this->prophesize(ViewExecutable::class);
    $display = $this->prophesize(DisplayPluginBase::class);
    $display->getOption('relationships')->willReturn(FALSE);
    $view->display_handler = $display->reveal();

    /** @var \Drupal\views\Plugin\ViewsHandlerManager $manager */
    $manager = $this->container->get('plugin.manager.views.filter');
    /** @var \Drupal\views\Plugin\views\filter\InOperator $operator */
    $operator = $manager->createInstance('in_operator');
    $options = ['value' => ['foo' => [], 'baz' => []]];
    $operator->init($view->reveal()$display->reveal()$options);

    $input_options = [
      'foo' => 'bar',
      'baz' => $this->t('qux'),
      'quux' => (object) ['option' => ['quux' => 'corge']],
    ];
    $reduced_values = $operator->reduceValueOptions($input_options);

    $this->assertSame(['foo', 'baz']array_keys($reduced_values));
    
parent::processDefinition($definition$plugin_id);
  }

  /** * {@inheritdoc} */
  public function getPlugin(string $plugin_id, ?EditorInterface $editor): CKEditor5PluginInterface {
    $configuration = $editor
      ? self::getPluginConfiguration($editor$plugin_id)
      : [];
    return $this->createInstance($plugin_id$configuration);
  }

  /** * Gets the plugin configuration (if any) from a text editor config entity. * * @param \Drupal\editor\EditorInterface $editor * A text editor config entity that is using CKEditor 5. * @param string $plugin_id * A CKEditor 5 plugin ID. * * @return array * The CKEditor 5 plugin configuration, if any. * * @throws \InvalidArgumentException * Thrown when the method is called with any other text editor than CKEditor 5. */
Home | Imprint | This part of the site doesn't use cookies.