hasFormClass example

$form['id'] = [
      '#type' => 'machine_name',
      '#default_value' => $this->stateId,
      '#machine_name' => [
        'exists' => [$this, 'exists'],
      ],
      '#disabled' => TRUE,
    ];

    // Add additional form fields from the workflow type plugin.     if ($workflow_type->hasFormClass(StateInterface::PLUGIN_FORM_KEY)) {
      $form['type_settings'] = [
        '#tree' => TRUE,
      ];
      $subform_state = SubformState::createForSubform($form['type_settings']$form$form_state);
      $subform_state->set('state', $state);
      $form['type_settings'] += $this->pluginFormFactory
        ->createInstance($workflow_type, StateInterface::PLUGIN_FORM_KEY)
        ->buildConfigurationForm($form['type_settings']$subform_state);
    }

    $header = [
      

    $form['to'] = [
      '#type' => 'radios',
      '#title' => $this->t('To'),
      '#required' => TRUE,
      '#default_value' => $transition->to()->id(),
      '#options' => $states,
      '#disabled' => TRUE,
    ];

    // Add additional form fields from the workflow type plugin.     if ($workflow_type->hasFormClass(TransitionInterface::PLUGIN_FORM_KEY)) {
      $form['type_settings'] = [
        '#tree' => TRUE,
      ];
      $subform_state = SubformState::createForSubform($form['type_settings']$form$form_state);
      $subform_state->set('transition', $transition);
      $form['type_settings'] += $this->pluginFormFactory
        ->createInstance($workflow_type, TransitionInterface::PLUGIN_FORM_KEY)
        ->buildConfigurationForm($form['type_settings']$subform_state);
    }

    return $form;
  }

  public function __construct(ClassResolverInterface $class_resolver) {
    $this->classResolver = $class_resolver;
  }

  /** * {@inheritdoc} */
  public function createInstance(PluginWithFormsInterface $plugin$operation$fallback_operation = NULL) {
    if (!$plugin->hasFormClass($operation)) {
      // Use the default form class if no form is specified for this operation.       if ($fallback_operation && $plugin->hasFormClass($fallback_operation)) {
        $operation = $fallback_operation;
      }
      else {
        throw new InvalidPluginDefinitionException($plugin->getPluginId()sprintf('The "%s" plugin did not specify a "%s" form class', $plugin->getPluginId()$operation));
      }
    }

    $form_class = $plugin->getFormClass($operation);

    
'#default_value' => '',
      '#required' => TRUE,
    ];

    $form['id'] = [
      '#type' => 'machine_name',
      '#machine_name' => [
        'exists' => [$this, 'exists'],
      ],
    ];

    if ($workflow_type->hasFormClass(StateInterface::PLUGIN_FORM_KEY)) {
      $form['type_settings'] = [
        '#tree' => TRUE,
      ];
      $subform_state = SubformState::createForSubform($form['type_settings']$form$form_state);
      $form['type_settings'] += $this->pluginFormFactory
        ->createInstance($workflow_type, StateInterface::PLUGIN_FORM_KEY)
        ->buildConfigurationForm($form['type_settings']$subform_state);
    }

    return $form;
  }

  
/** * @covers ::createInstance */
  public function testCreateInstance() {
    $plugin_form = $this->prophesize(PluginFormInterface::class);
    $expected = $plugin_form->reveal();

    $this->classResolver->getInstanceFromDefinition(get_class($expected))->willReturn($expected);

    $plugin = $this->prophesize(PluginWithFormsInterface::class);
    $plugin->hasFormClass('standard_class')->willReturn(TRUE);
    $plugin->getFormClass('standard_class')->willReturn(get_class($expected));

    $form_object = $this->manager->createInstance($plugin->reveal(), 'standard_class');
    $this->assertSame($expected$form_object);
  }

  /** * @covers ::createInstance */
  public function testCreateInstanceUsingPlugin() {
    $this->classResolver->getInstanceFromDefinition(Argument::cetera())->shouldNotBeCalled();

    

class PluginWithFormsTraitTest extends UnitTestCase {

  /** * @covers ::getFormClass * @covers ::hasFormClass * @dataProvider providerGetFormClass */
  public function testGetFormClass(PluginWithFormsInterface $block_plugin$operation$expected_class) {
    $this->assertSame($expected_class$block_plugin->getFormClass($operation));
    $this->assertSame($expected_class !== NULL, $block_plugin->hasFormClass($operation));
  }

  /** * @return array */
  public function providerGetFormClass() {
    $block_plugin_without_forms = new TestClass([], 'block_plugin_without_forms', [
      'provider' => 'block_test',
    ]);
    // A block plugin that has a form defined for the 'poke' operation.     $block_plugin_with_forms = new TestClass([], 'block_plugin_with_forms', [
      
'#default_value' => [],
      '#options' => $states,
    ];
    $form['to'] = [
      '#type' => 'radios',
      '#title' => $this->t('To'),
      '#required' => TRUE,
      '#options' => $states,
    ];

    // Add additional form fields from the workflow type plugin.     if ($workflow_type->hasFormClass(TransitionInterface::PLUGIN_FORM_KEY)) {
      $form['type_settings'] = [
        '#tree' => TRUE,
      ];
      $subform_state = SubformState::createForSubform($form['type_settings']$form$form_state);
      $form['type_settings'] += $this->pluginFormFactory
        ->createInstance($workflow_type, TransitionInterface::PLUGIN_FORM_KEY)
        ->buildConfigurationForm($form['type_settings']$subform_state);
    }

    return $form;
  }

  
'to' => ['#markup' => $transition->to()->label()],
        'operations' => [
          '#type' => 'operations',
          '#links' => $links,
        ],
      ];
    }
    $form['transitions_container']['transition_add'] = [
      '#markup' => $workflow->toLink($this->t('Add a new transition'), 'add-transition-form')->toString(),
    ];

    if ($workflow_type->hasFormClass(WorkflowTypeInterface::PLUGIN_FORM_KEY)) {
      $form['type_settings'] = [
        '#tree' => TRUE,
      ];
      $subform_state = SubformState::createForSubform($form['type_settings']$form$form_state);
      $form['type_settings'] += $this->pluginFormFactory
        ->createInstance($workflow_type, WorkflowTypeInterface::PLUGIN_FORM_KEY)
        ->buildConfigurationForm($form['type_settings']$subform_state);
    }

    return $form;
  }

  

  public function accessBlockPlugin(BlockPluginInterface $block_plugin) {
    return AccessResult::allowedIf($block_plugin instanceof PluginWithFormsInterface && $block_plugin->hasFormClass('settings_tray'));
  }

}
/** * @covers ::access * @covers ::accessBlockPlugin * @dataProvider providerTestAccess */
  public function testAccess($with_forms, array $plugin_definition, AccessResultInterface $expected_access_result) {
    $block_plugin = $this->prophesize()->willImplement(BlockPluginInterface::class);

    if ($with_forms) {
      $block_plugin->willImplement(PluginWithFormsInterface::class);
      $block_plugin->hasFormClass(Argument::type('string'))->will(function D$arguments) use ($plugin_definition) {
        return !empty($plugin_definition['forms'][$arguments[0]]);
      });
    }

    $block = $this->prophesize(BlockInterface::class);
    $block->getPlugin()->willReturn($block_plugin->reveal());

    $access_check = new BlockPluginHasSettingsTrayFormAccessCheck();
    $this->assertEquals($expected_access_result$access_check->access($block->reveal()));
    $this->assertEquals($expected_access_result$access_check->accessBlockPlugin($block_plugin->reveal()));
  }

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