当前位置: 首页>>代码示例>>PHP>>正文


PHP Form\FormState类代码示例

本文整理汇总了PHP中Drupal\Core\Form\FormState的典型用法代码示例。如果您正苦于以下问题:PHP FormState类的具体用法?PHP FormState怎么用?PHP FormState使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了FormState类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: buildChildFormState

  /**
   * Build all necessary things for child form (form state, etc.).
   *
   * @param \Drupal\Core\Entity\EntityFormInterface $controller
   *   Entity form controller for child form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   Parent form state object.
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   Entity object.
   * @param string $operation
   *   Operation that is to be performed in inline form.
   * @param array $parents
   *   Entity form #parents.
   *
   * @return \Drupal\Core\Form\FormStateInterface
   *   Child form state object.
   */
  public static function buildChildFormState(EntityFormInterface $controller, FormStateInterface $form_state, EntityInterface $entity, $operation, $parents) {
    $child_form_state = new FormState();

    $child_form_state->addBuildInfo('callback_object', $controller);
    $child_form_state->addBuildInfo('base_form_id', $controller->getBaseFormID());
    $child_form_state->addBuildInfo('form_id', $controller->getFormID());
    $child_form_state->addBuildInfo('args', array());

    // Copy values to child form.
    $child_form_state->setCompleteForm($form_state->getCompleteForm());
    $child_form_state->setUserInput($form_state->getUserInput());

    // Filter out all submitted values that are not directly relevant for this
    // IEF. Otherwise they might mess things up.
    $form_state_values = $form_state->getValues();
    $form_state_values = static::extractArraySequence($form_state_values, $parents);

    $child_form_state->setValues($form_state_values);
    $child_form_state->setStorage($form_state->getStorage());
    $value = \Drupal::entityTypeManager()->getStorage('entity_form_display')->load($entity->getEntityTypeId() . '.' . $entity->bundle() . '.' . $operation);
    $child_form_state->set('form_display', $value);

    // Since some of the submit handlers are run, redirects need to be disabled.
    $child_form_state->disableRedirect();

    // When a form is rebuilt after Ajax processing, its #build_id and #action
    // should not change.
    // @see drupal_rebuild_form()
    $rebuild_info = $child_form_state->getRebuildInfo();
    $rebuild_info['copy']['#build_id'] = TRUE;
    $rebuild_info['copy']['#action'] = TRUE;
    $child_form_state->setRebuildInfo($rebuild_info);

    $child_form_state->set('inline_entity_form', $form_state->get('inline_entity_form'));
    $child_form_state->set('langcode', $entity->language()->getId());

    $child_form_state->set('field', $form_state->get('field'));
    $child_form_state->setTriggeringElement($form_state->getTriggeringElement());
    $child_form_state->setSubmitHandlers($form_state->getSubmitHandlers());

    return $child_form_state;
  }
开发者ID:joebachana,项目名称:usatne,代码行数:59,代码来源:EntityInlineForm.php

示例2: featureEdit

 /**
  * Displays the edit feature form.
  */
 public function featureEdit(NodeInterface $node, $fid, $pfid)
 {
     $func = uc_product_feature_data($fid, 'callback');
     $form_state = new FormState();
     $form_state->setBuildInfo(array('args' => array($node, uc_product_feature_load($pfid))));
     return $this->formBuilder()->buildForm($func, $form_state);
 }
开发者ID:justincletus,项目名称:webdrupalpro,代码行数:10,代码来源:ProductFeaturesController.php

示例3: testProcessMachineName

  /**
   * @covers ::processMachineName
   */
  public function testProcessMachineName() {
    $form_state = new FormState();

    $element = [
      '#id' => 'test',
      '#field_suffix' => 'test_suffix',
      '#field_prefix' => 'test_prefix',
      '#machine_name' => [
        'source' => [
          'test_source',
        ],
        'maxlength' => 32,
        'additional_property' => TRUE,
        '#additional_property_with_hash' => TRUE,
      ]
    ];

    $complete_form = [
      'test_source' => [
        '#type' => 'textfield',
        '#id' => 'source',
      ],
      'test_machine_name' => $element
    ];

    $form_state->setCompleteForm($complete_form);

    $language = $this->prophesize(LanguageInterface::class);
    $language->getId()->willReturn('xx-lolspeak');

    $language_manager = $this->prophesize(LanguageManagerInterface::class);
    $language_manager->getCurrentLanguage()->willReturn($language);

    $csrf_token = $this->prophesize(CsrfTokenGenerator::class);
    $csrf_token->get('[^a-z0-9_]+')->willReturn('tis-a-fine-token');

    $container = $this->prophesize(ContainerInterface::class);
    $container->get('language_manager')->willReturn($language_manager->reveal());
    $container->get('csrf_token')->willReturn($csrf_token->reveal());
    \Drupal::setContainer($container->reveal());

    $element = MachineName::processMachineName($element, $form_state, $complete_form);
    $settings = $element['#attached']['drupalSettings']['machineName']['#source'];

    $allowed_options = [
      'replace_pattern',
      'replace',
      'maxlength',
      'target',
      'label',
      'field_prefix',
      'field_suffix',
      'suffix',
      'replace_token',
    ];
    $this->assertEmpty(array_diff_key($settings, array_flip($allowed_options)));
    foreach ($allowed_options as $key) {
      $this->assertArrayHasKey($key, $settings);
    }
  }
开发者ID:Greg-Boggs,项目名称:electric-dev,代码行数:63,代码来源:MachineNameTest.php

示例4: getForm

 /**
  * {@inheritdoc}
  */
 public function getForm($formClass)
 {
     $args = func_get_args();
     array_shift($args);
     if (!class_exists($formClass)) {
         $this->logger->log(LogLevel::CRITICAL, "Form class '@class' does not exists", ['@class' => $formClass]);
         return [];
     }
     if (!method_exists($formClass, 'create')) {
         $this->logger->log(LogLevel::CRITICAL, "Form class '@class' does not implements ::create()", ['@class' => $formClass]);
         return [];
     }
     // God, I do hate Drupal 8...
     $form = call_user_func([$formClass, 'create'], $this->container);
     if (!$form instanceof FormInterface) {
         $this->logger->log(LogLevel::CRITICAL, "Form class '@class' does not implement \\Drupal\\Core\\Form\\FormInterface", ['@class' => $formClass]);
         return [];
     }
     $formId = $form->getFormId();
     $data = [];
     $data['build_info']['args'] = $args;
     $formState = new FormState($data);
     $formState->setFormObject($form);
     $this->formMap[$formId] = [$form, $formState];
     return drupal_build_form($formId, $data);
 }
开发者ID:makinacorpus,项目名称:drupal-sf-dic,代码行数:29,代码来源:FormBuilder.php

示例5: submitFormPage

 /**
  * Page that triggers a programmatic form submission.
  *
  * Returns the validation errors triggered by the form submission as json.
  */
 public function submitFormPage()
 {
     $form_state = new FormState();
     $values = ['name' => 'robo-user', 'mail' => 'robouser@example.com', 'op' => t('Submit')];
     $form_state->setValues($values);
     \Drupal::formBuilder()->submitForm('\\Drupal\\user\\Form\\UserPasswordForm', $form_state);
     return new JsonResponse($form_state->getErrors());
 }
开发者ID:systemick3,项目名称:systemick.co.uk,代码行数:13,代码来源:HoneypotTestController.php

示例6: testValidationComplete

 /**
  * Tests the 'validation_complete' $form_state flag.
  *
  * @covers ::validateForm
  * @covers ::finalizeValidation
  */
 public function testValidationComplete()
 {
     $form_validator = $this->getMockBuilder('Drupal\\Core\\Form\\FormValidator')->setConstructorArgs([new RequestStack(), $this->getStringTranslationStub(), $this->csrfToken, $this->logger, $this->formErrorHandler])->setMethods(NULL)->getMock();
     $form = array();
     $form_state = new FormState();
     $this->assertFalse($form_state->isValidationComplete());
     $form_validator->validateForm('test_form_id', $form, $form_state);
     $this->assertTrue($form_state->isValidationComplete());
 }
开发者ID:neetumorwani,项目名称:blogging,代码行数:15,代码来源:FormValidatorTest.php

示例7: testValidationComplete

 /**
  * Tests the 'validation_complete' $form_state flag.
  *
  * @covers ::validateForm
  * @covers ::finalizeValidation
  */
 public function testValidationComplete()
 {
     $form_validator = $this->getMockBuilder('Drupal\\Core\\Form\\FormValidator')->disableOriginalConstructor()->setMethods(NULL)->getMock();
     $form = array();
     $form_state = new FormState();
     $this->assertFalse($form_state->isValidationComplete());
     $form_validator->validateForm('test_form_id', $form, $form_state);
     $this->assertTrue($form_state->isValidationComplete());
 }
开发者ID:davidsoloman,项目名称:drupalconsole.com,代码行数:15,代码来源:FormValidatorTest.php

示例8: testSetElementErrorsFromFormState

 /**
  * @covers ::handleFormErrors
  * @covers ::setElementErrorsFromFormState
  */
 public function testSetElementErrorsFromFormState()
 {
     $form_error_handler = $this->getMockBuilder('Drupal\\Core\\Form\\FormErrorHandler')->setMethods(['drupalSetMessage'])->getMock();
     $form = ['#parents' => []];
     $form['test'] = ['#type' => 'textfield', '#title' => 'Test', '#parents' => ['test'], '#id' => 'edit-test'];
     $form_state = new FormState();
     $form_state->setErrorByName('test', 'invalid');
     $form_error_handler->handleFormErrors($form, $form_state);
     $this->assertSame('invalid', $form['test']['#errors']);
 }
开发者ID:aWEBoLabs,项目名称:taxi,代码行数:14,代码来源:FormErrorHandlerTest.php

示例9: testSetElementErrorsFromFormState

 /**
  * @covers ::handleFormErrors
  * @covers ::setElementErrorsFromFormState
  */
 public function testSetElementErrorsFromFormState()
 {
     $form_error_handler = $this->getMockBuilder('Drupal\\Core\\Form\\FormErrorHandler')->setConstructorArgs([$this->getStringTranslationStub(), $this->getMock('Drupal\\Core\\Utility\\LinkGeneratorInterface')])->setMethods(['drupalSetMessage'])->getMock();
     $form = ['#parents' => []];
     $form['test'] = ['#type' => 'textfield', '#title' => 'Test', '#parents' => ['test'], '#id' => 'edit-test'];
     $form_state = new FormState();
     $form_state->setErrorByName('test', 'invalid');
     $form_error_handler->handleFormErrors($form, $form_state);
     $this->assertSame('invalid', $form['test']['#errors']);
 }
开发者ID:nsp15,项目名称:Drupal8,代码行数:14,代码来源:FormErrorHandlerTest.php

示例10: testGetSelected

 /**
  * @covers ::getSelected
  *
  * @dataProvider providerTestGetSelected
  */
 public function testGetSelected($expected, $element = [], $parents = [], $user_input = [], $not_rebuilding_expected = NULL)
 {
     $not_rebuilding_expected = $not_rebuilding_expected ?: $expected;
     $form_state = new FormState();
     $form_state->setUserInput($user_input);
     $actual = WizardPluginBase::getSelected($form_state, $parents, 'the_default_value', $element);
     $this->assertSame($not_rebuilding_expected, $actual);
     $this->assertSame($user_input, $form_state->getUserInput());
     $form_state->setRebuild();
     $actual = WizardPluginBase::getSelected($form_state, $parents, 'the_default_value', $element);
     $this->assertSame($expected, $actual);
     $this->assertSame($user_input, $form_state->getUserInput());
 }
开发者ID:aWEBoLabs,项目名称:taxi,代码行数:18,代码来源:WizardPluginBaseTest.php

示例11: testLimitValidationErrors

 /**
  * Tests that #limit_validation_errors of the only submit button takes effect.
  */
 function testLimitValidationErrors()
 {
     // Programmatically submit the form.
     $form_state = new FormState();
     $form_state->setValue('section', 'one');
     $form_builder = $this->container->get('form_builder');
     $form_builder->submitForm($this, $form_state);
     // Verify that only the specified section was validated.
     $errors = $form_state->getErrors();
     $this->assertTrue(isset($errors['one']), "Section 'one' was validated.");
     $this->assertFalse(isset($errors['two']), "Section 'two' was not validated.");
     // Verify that there are only values for the specified section.
     $this->assertTrue($form_state->hasValue('one'), "Values for section 'one' found.");
     $this->assertFalse($form_state->hasValue('two'), "Values for section 'two' not found.");
 }
开发者ID:ddrozdik,项目名称:dmaps,代码行数:18,代码来源:TriggeringElementProgrammedUnitTest.php

示例12: testInstallConfigureForm

 /**
  * Tests the root user account form section in the "Configure site" form.
  */
 function testInstallConfigureForm()
 {
     require_once \Drupal::root() . '/core/includes/install.core.inc';
     require_once \Drupal::root() . '/core/includes/install.inc';
     $install_state = install_state_defaults();
     $form_state = new FormState();
     $form_state->addBuildInfo('args', [&$install_state]);
     $form = $this->container->get('form_builder')->buildForm('Drupal\\Core\\Installer\\Form\\SiteConfigureForm', $form_state);
     // Verify name and pass field order.
     $this->assertFieldOrder($form['admin_account']['account']);
     // Verify that web browsers may autocomplete the email value and
     // autofill/prefill the name and pass values.
     foreach (array('mail', 'name', 'pass') as $key) {
         $this->assertFalse(isset($form['account'][$key]['#attributes']['autocomplete']), "'{$key}' field: 'autocomplete' attribute not found.");
     }
 }
开发者ID:ravibarnwal,项目名称:laraitassociate.in,代码行数:19,代码来源:UserAccountFormFieldsTest.php

示例13: testEntityRow

 /**
  * Tests the entity row handler.
  */
 public function testEntityRow()
 {
     $vocab = Vocabulary::create(['name' => $this->randomMachineName(), 'vid' => strtolower($this->randomMachineName())]);
     $vocab->save();
     $term = Term::create(['name' => $this->randomMachineName(), 'vid' => $vocab->id()]);
     $term->save();
     $view = Views::getView('test_entity_row');
     $build = $view->preview();
     $this->render($build);
     $this->assertText($term->getName(), 'The rendered entity appears as row in the view.');
     // Tests the available view mode options.
     $form = array();
     $form_state = new FormState();
     $form_state->set('view', $view->storage);
     $view->rowPlugin->buildOptionsForm($form, $form_state);
     $this->assertTrue(isset($form['view_mode']['#options']['default']), 'Ensure that the default view mode is available');
 }
开发者ID:sojo,项目名称:d8_friendsofsilence,代码行数:20,代码来源:RowEntityTest.php

示例14: testExtractFormValues

 /**
  * @covers ::extractFormValues
  */
 public function testExtractFormValues()
 {
     $menu_link_manager = $this->prophesize(MenuLinkManagerInterface::class);
     $menu_parent_form_selector = $this->prophesize(MenuParentFormSelectorInterface::class);
     $module_handler = $this->prophesize(ModuleHandlerInterface::class);
     $menu_link_form = new MenuLinkDefaultForm($menu_link_manager->reveal(), $menu_parent_form_selector->reveal(), $this->getStringTranslationStub(), $module_handler->reveal());
     $static_override = $this->prophesize(StaticMenuLinkOverridesInterface::class);
     $menu_link = new MenuLinkDefault([], 'my_plugin_id', [], $static_override->reveal());
     $menu_link_form->setMenuLinkInstance($menu_link);
     $form_state = new FormState();
     $form_state->setValue('id', 'my_plugin_id');
     $form_state->setValue('enabled', FALSE);
     $form_state->setValue('weight', 5);
     $form_state->setValue('expanded', TRUE);
     $form_state->setValue('menu_parent', 'foo:bar');
     $form = [];
     $result = $menu_link_form->extractFormValues($form, $form_state);
     $this->assertEquals(['id' => 'my_plugin_id', 'enabled' => 0, 'weight' => 5, 'expanded' => 1, 'parent' => 'bar', 'menu_name' => 'foo'], $result);
 }
开发者ID:frankcr,项目名称:sftw8,代码行数:22,代码来源:MenuLinkDefaultFormTest.php

示例15: testSetCacheWithSafeStrings

 /**
  * @covers ::setCache
  */
 public function testSetCacheWithSafeStrings()
 {
     // A call to SafeMarkup::set() is appropriate in this test as a way to add a
     // string to the safe list in the simplest way possible. Normally, avoid it.
     SafeMarkup::set('a_safe_string');
     $form_build_id = 'the_form_build_id';
     $form = ['#form_id' => 'the_form_id'];
     $form_state = new FormState();
     $this->formCacheStore->expects($this->once())->method('setWithExpire')->with($form_build_id, $form, $this->isType('int'));
     $form_state_data = $form_state->getCacheableArray();
     $form_state_data['build_info']['safe_strings'] = ['a_safe_string' => ['html' => TRUE]];
     $this->formStateCacheStore->expects($this->once())->method('setWithExpire')->with($form_build_id, $form_state_data, $this->isType('int'));
     $this->formCache->setCache($form_build_id, $form, $form_state);
 }
开发者ID:nsp15,项目名称:Drupal8,代码行数:17,代码来源:FormCacheTest.php


注:本文中的Drupal\Core\Form\FormState类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。