/** * {@inheritdoc} */ protected function buildSelector(array $root_element, FormStateInterface $form_state, array $plugins) { $element = parent::buildSelector($root_element, $form_state, $plugins); /** @var \Drupal\Component\Plugin\PluginInspectionInterface[] $plugins */ $element['container']['plugin_id'] = array('#ajax' => array('callback' => array(get_class(), 'ajaxRebuildForm'), 'effect' => 'fade', 'event' => 'change', 'trigger_as' => array('name' => $element['container']['change']['#name'])), '#default_value' => $this->getSelectedPlugin() ? $this->getSelectedPlugin()->getPluginId() : NULL, '#empty_value' => '', '#options' => $this->buildOptionsLevel($this->buildPluginHierarchy($this->selectablePluginDiscovery)), '#required' => $this->isRequired(), '#title' => $this->getLabel(), '#description' => $this->getDescription(), '#type' => 'select'); return $element; }
/** * {@inheritdoc} */ protected function buildSelector(array $root_element, FormStateInterface $form_state, array $plugins) { $element = parent::buildSelector($root_element, $form_state, $plugins); /** @var \Drupal\Component\Plugin\PluginInspectionInterface[] $plugins */ $plugin_options = []; foreach ($plugins as $plugin) { $plugin_definition = $this->selectablePluginType->ensureTypedPluginDefinition($plugin->getPluginDefinition()); $plugin_options[$plugin->getPluginId()] = $plugin_definition instanceof PluginLabelDefinitionInterface ? $plugin_definition->getLabel() : $plugin_definition->getId(); } natcasesort($plugin_options); $element['container']['plugin_id'] = array('#ajax' => array('callback' => array(get_class(), 'ajaxRebuildForm'), 'effect' => 'fade', 'event' => 'change', 'progress' => 'none', 'trigger_as' => array('name' => $element['container']['change']['#name'])), '#attached' => ['library' => ['plugin/plugin_selector.plugin_radios']], '#default_value' => $this->getSelectedPlugin() ? $this->getSelectedPlugin()->getPluginId() : NULL, '#empty_value' => 'select', '#options' => $plugin_options, '#required' => $this->isRequired(), '#title' => $this->getlabel(), '#description' => $this->getDescription(), '#type' => 'radios'); return $element; }
/** * @covers ::buildSelector */ public function testBuildSelector() { $this->stringTranslation->expects($this->any())->method('translate')->willReturnArgument(0); $method = new \ReflectionMethod($this->sut, 'buildSelector'); $method->setAccessible(TRUE); $plugin_id = $this->randomMachineName(); $plugin_label = $this->randomMachineName(); $plugin = $this->getMock(PluginInspectionInterface::class); $plugin->expects($this->any())->method('getPluginId')->willReturn($plugin_id); $plugin->expects($this->any())->method('getPluginLabel')->willReturn($plugin_label); $this->sut->setSelectedPlugin($plugin); $element = array('#parents' => array('foo', 'bar')); $form_state = $this->getMock(FormStateInterface::class); $available_plugins = array($plugin); $expected_build_change = array('#ajax' => array('callback' => array(AdvancedPluginSelectorBase::class, 'ajaxRebuildForm')), '#attributes' => array('class' => array('js-hide')), '#limit_validation_errors' => array(array('foo', 'bar', 'select', 'plugin_id')), '#name' => 'foo[bar][select][container][change]', '#submit' => [[AdvancedPluginSelectorBase::class, 'rebuildForm']], '#type' => 'submit', '#value' => 'Choose'); $build = $method->invokeArgs($this->sut, array($element, $form_state, $available_plugins)); $this->assertArrayHasKey('plugin_id', $build['container']); $this->assertEquals($expected_build_change, $build['container']['change']); $this->assertSame('container', $build['container']['#type']); }