Esempio n. 1
1
  /**
   * 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;
  }
Esempio n. 2
0
 /**
  * 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');
 }
 /**
  * @covers ::actions
  * @covers ::getPaymentMethodManager
  */
 public function testActionsWithoutAvailablePlugins()
 {
     $form = [];
     $form_state = new FormState();
     $form_state->set('plugin_selector', $this->pluginSelector);
     $this->paymentMethodManager->expects($this->atLeastOnce())->method('getDefinitions')->willReturn([]);
     $method = new \ReflectionMethod($this->sut, 'actions');
     $method->setAccessible(TRUE);
     $actions = $method->invokeArgs($this->sut, [$form, $form_state]);
     $this->assertTrue($actions['submit']['#disabled']);
 }
 /**
  * {@inheritdoc}
  */
 public function submitForm(array &$form, FormStateInterface $form_state)
 {
     $form_values = $form_state->getValues();
     $plugin_settings = $form_state->get('plugin_settings');
     $field_types = $form_values['fields'];
     // Remove from configuration the keys of the field types which have no
     // plugin selected. We need to clear this keys from configuration first
     // and then save the settings for the fields which have a plugin selected.
     // If we do both writing and clearing in the same for, the values won't get
     // saved.
     foreach ($field_types as $field_type => $field_type_values) {
         // If there is no plugin selected remove the key from config file.
         if ($field_type_values['plugin']['type'] == 'hidden') {
             $this->config->clear($field_type);
         }
     }
     $this->config->save();
     // For field types that have a plugin selected save the settings.
     foreach ($field_types as $field_type => $field_type_values) {
         if ($field_type_values['plugin']['type'] != 'hidden') {
             // Get plugin settings. They lie either directly in submitted form
             // values (if the whole form was submitted while some plugin settings
             // were being edited), or have been persisted in $form_state.
             $plugin = $this->diffBuilderManager->createInstance($field_type_values['plugin']['type']);
             // Form submitted without pressing update button on plugin settings form.
             if (isset($field_type_values['settings_edit_form']['settings'])) {
                 $settings = $field_type_values['settings_edit_form']['settings'];
             } elseif (isset($plugin_settings[$field_type]['settings'])) {
                 $settings = $plugin_settings[$field_type]['settings'];
             } else {
                 $settings = $plugin->defaultConfiguration();
             }
             // Build a FormState object and call the plugin submit handler.
             $state = new FormState();
             $state->setValues($settings);
             $state->set('field_type', $field_type);
             $plugin->submitConfigurationForm($form, $state);
         }
     }
     drupal_set_message($this->t('Your settings have been saved.'));
 }
 /**
  * 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.
  *
  * @return \Drupal\Core\Form\FormStateInterface
  *   Child form state object.
  */
 public static function buildChildFormState(EntityFormInterface $controller, FormStateInterface $form_state, EntityInterface $entity, $operation)
 {
     $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->setUserInput($form_state->getUserInput());
     $child_form_state->setValues($form_state->getValues());
     $child_form_state->setStorage($form_state->getStorage());
     $child_form_state->set('form_display', entity_get_form_display($entity->getEntityTypeId(), $entity->bundle(), $operation));
     // 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;
 }
 /**
  * Get the wizard form state.
  *
  * @param \Drupal\ctools\Wizard\FormWizardInterface $wizard
  *   The form wizard.
  * @param array $parameters
  *   The array of parameters specific to this wizard.
  * @param bool $ajax
  *
  * @return \Drupal\Core\Form\FormState
  */
 public function getFormState(FormWizardInterface $wizard, array $parameters, $ajax = FALSE)
 {
     $form_state = new FormState();
     // If a wizard has no values, initialize them.
     if (!$wizard->getTempstore()->get($wizard->getMachineName())) {
         $cached_values = $wizard->initValues();
         // Save the cached values that were initialized.
         //$wizard->getTempstore()->set($wizard->getMachineName(), $cached_values);
     } else {
         $cached_values = $wizard->getTempstore()->get($wizard->getMachineName());
     }
     $form_state->setTemporaryValue('wizard', $cached_values);
     $form_state->set('ajax', $ajax);
     $parameters['form'] = [];
     $parameters['form_state'] = $form_state;
     $method = new \ReflectionMethod($wizard, 'buildForm');
     $arguments = [];
     foreach ($method->getParameters() as $parameter) {
         if (array_key_exists($parameter->name, $parameters)) {
             $arguments[] = $parameters[$parameter->name];
         } elseif ($parameter->isDefaultValueAvailable()) {
             $arguments[] = $parameter->getDefaultValue();
         }
     }
     unset($parameters['form'], $parameters['form_state']);
     // Remove $form and $form_state from the arguments, and re-index them.
     unset($arguments[0], $arguments[1]);
     $form_state->addBuildInfo('args', array_values($arguments));
     return $form_state;
 }
 /**
  * @covers ::validate
  */
 public function testValidate()
 {
     $line_item_name_a = $this->randomMachineName();
     $line_item_name_b = $this->randomMachineName();
     $line_item_name_c = $this->randomMachineName();
     $root_element_name = $this->randomMachineName();
     $form_build = array('foo' => array('#name' => $root_element_name, '#parents' => array('foo'), 'line_items' => []));
     $line_item_a = $this->getMock(PaymentLineItemInterface::class);
     $line_item_b = $this->getMock(PaymentLineItemInterface::class);
     $line_item_c = $this->getMock(PaymentLineItemInterface::class);
     /** @var \PHPUnit_Framework_MockObject_MockObject[] $line_items */
     $line_items = array($line_item_name_a => $line_item_a, $line_item_name_b => $line_item_b, $line_item_name_c => $line_item_c);
     foreach ($line_items as $line_item_name => $line_item) {
         $form_build['foo']['line_items'][$line_item_name] = array('plugin_form' => array('#foo' => $this->randomMachineName()));
         $line_item->expects($this->atLeastOnce())->method('getName')->willReturn($line_item_name);
         $line_item->expects($this->once())->method('validateConfigurationForm')->with($form_build['foo']['line_items'][$line_item_name]['plugin_form']);
         $line_item->expects($this->once())->method('submitConfigurationForm')->with($form_build['foo']['line_items'][$line_item_name]['plugin_form']);
     }
     $form_state = new FormState();
     $form_state->set('payment.element.payment_line_items_input.configured.' . $root_element_name, array_values($line_items));
     $form_state->setValues(array('foo' => array('line_items' => array($line_item_name_a => array('weight' => 3), $line_item_name_b => array('weight' => 1), $line_item_name_c => array('weight' => 2)))));
     $this->sut->validate($form_build['foo'], $form_state, $form_build);
     $element = $this->sut;
     $line_items = $element::getLineItems($form_build['foo'], $form_state);
     $this->assertSame(array($line_item_b, $line_item_c, $line_item_a), $line_items);
 }
 /**
  * @covers ::ajaxPay
  *
  * @dataProvider providerTestAjaxPay
  */
 public function testAjaxPay($is_completed, $number_of_commands)
 {
     $payment = $this->getMock(PaymentInterface::class);
     $result = $this->getMock(OperationResultInterface::class);
     $result->expects($this->atLeastOnce())->method('isCompleted')->willReturn($is_completed);
     $payment_method = $this->getMock(PaymentMethodInterface::class);
     $payment_method->expects($this->any())->method('getPayment')->willReturn($payment);
     $payment_method->expects($this->atLeastOnce())->method('getPaymentExecutionResult')->willReturn($result);
     $plugin_selector_plugin_id = $this->randomMachineName();
     $plugin_selector = $this->getMock(PluginSelectorInterface::class);
     $plugin_selector->expects($this->atLeastOnce())->method('getSelectedPlugin')->willReturn($payment_method);
     $form = array('foo' => array('bar' => array('#limit_allowed_plugin_ids' => [], '#name' => $this->randomMachineName(), '#plugin_selector_id' => $plugin_selector_plugin_id, '#prototype_payment' => $payment, '#required' => TRUE, 'container' => array('#id' => $this->randomMachineName(), 'payment_form' => array('pay' => array('#array_parents' => array('foo', 'bar', 'container', 'payment_form', 'pay')))))));
     $form_state = new FormState();
     $form_state->set('payment_reference.element.payment_reference.plugin_selector.' . $form['foo']['bar']['#name'], $plugin_selector);
     $form_state->setTriggeringElement($form['foo']['bar']['container']['payment_form']['pay']);
     $response = $this->sut->ajaxPay($form, $form_state);
     $this->assertInstanceOf(AjaxResponse::class, $response);
     $this->assertCount($number_of_commands, $response->getCommands());
 }