/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { // Nest the element to make sure that works. $form['container']['line_item'] = array('#cardinality' => 4, '#default_value' => Generate::createPaymentLineItems(), '#type' => 'payment_line_items_input'); $form['submit'] = array('#type' => 'submit', '#value' => t('Submit')); return $form; }
/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { $key = 'payment_reference_element_prototype_payment'; if ($form_state->has($key)) { $prototype_payment = $form_state->get($key); /** @var \Drupal\payment_reference\Plugin\Payment\Type\PaymentReference $payment_type */ $payment_type = $prototype_payment->getPaymentType(); } else { $entity_type_id = 'user'; $bundle = 'user'; $field_name = 'foobarbaz'; /** @var \Drupal\payment\Entity\PaymentInterface $prototype_payment */ $prototype_payment = entity_create('payment', array('bundle' => 'payment_reference')); $prototype_payment->setCurrencyCode('EUR')->setOwnerId(2)->setLineItems(Generate::createPaymentLineItems()); /** @var \Drupal\payment_reference\Plugin\Payment\Type\PaymentReference $payment_type */ $payment_type = $prototype_payment->getPaymentType(); $payment_type->setEntityTypeId($entity_type_id); $payment_type->setBundle($bundle); $payment_type->setFieldName($field_name); $form_state->set($key, $prototype_payment); } $form['payment_reference'] = array('#plugin_selector_id' => 'payment_select_list', '#prototype_payment' => $prototype_payment, '#queue_category_id' => $payment_type->getEntityTypeId() . '.' . $payment_type->getBundle() . '.' . $payment_type->getFieldName(), '#queue_owner_id' => 2, '#required' => TRUE, '#title' => 'FooBarBaz', '#type' => 'payment_reference'); $form['submit'] = array('#type' => 'submit', '#value' => t('Submit')); return $form; }
/** * Asserts a correct field value. */ protected function assertFieldValue(EntityInterface $entity, $field_name) { $field = $entity->{$field_name}; foreach (Generate::createPaymentLineItems() as $i => $line_item) { $this->assertTrue(is_string($field[$i]->plugin_id)); $this->assertTrue(is_array($field[$i]->plugin_configuration)); } }
/** * Tests the element. */ protected function testElement() { $state = \Drupal::state(); $names = []; foreach (Generate::createPaymentLineItems() as $line_item) { $names[] = $line_item->getName(); } $type = 'payment_basic'; // Test the presence of default elements. $this->drupalGet('payment_test-element-payment-line-item'); $this->assertLineItemElements($names); $this->assertAddMore(TRUE); // Add a line item through a regular submission. $this->drupalPostForm(NULL, array('line_item[add_more][type]' => $type), t('Add and configure a new line item')); $this->assertLineItemElements(array_merge($names, array($type))); $this->assertAddMore(FALSE); // Delete a line item through a regular submission. $this->drupalPostForm(NULL, [], t('Delete')); $this->assertLineItemElements($names); $elements = $this->xpath('//input[@name="line_item[line_items][' . $type . '][weight]"]'); $this->assertFalse(isset($elements[0])); $this->assertAddMore(TRUE); // Change a line item's weight and test the element's value through a // regular submission. $name = 'line_item[line_items][' . reset($names) . '][weight]'; $this->assertFieldByXPath('//select[@name="' . $name . '"]/option[@value="0" and @selected="selected"]'); $this->drupalPostForm(NULL, array($name => count($names)), t('Submit')); $value = $state->get('payment_test_line_item_form_element'); if ($this->assertTrue(is_array($value))) { /// We end up with one more line item than we originally had. $this->assertEqual(count($value), count($names)); $line_items = []; foreach ($value as $line_item_data) { $this->assertTrue(isset($line_item_data['plugin_configuration'])); $this->assertTrue(is_array($line_item_data['plugin_configuration'])); $this->assertTrue(isset($line_item_data['plugin_id'])); $line_items[] = Payment::lineItemManager()->createInstance($line_item_data['plugin_id'], $line_item_data['plugin_configuration']); } // Check that the first line item is now the last. $this->assertEqual(end($line_items)->getName(), reset($names)); } }
/** * {@inheritdoc */ protected function setUp() { parent::setUp(); $this->paymentStorage = \Drupal::entityManager()->getStorage('payment'); /** @var \Drupal\currency\ConfigImporterInterface $config_importer */ $config_importer = \Drupal::service('currency.config_importer'); $config_importer->importCurrency('EUR'); // Create the field and field instance. $field_name = strtolower($this->randomMachineName()); entity_create('field_storage_config', ['cardinality' => FieldStorageConfigInterface::CARDINALITY_UNLIMITED, 'entity_type' => 'user', 'field_name' => $field_name, 'type' => 'payment_form'])->save(); entity_create('field_config', ['bundle' => 'user', 'entity_type' => 'user', 'field_name' => $field_name, 'settings' => ['currency_code' => 'EUR']])->save(); entity_get_display('user', 'user', 'default')->setComponent($field_name, ['type' => 'payment_form'])->save(); // Create an entity. $this->user = entity_create('user', ['name' => $this->randomString(), 'status' => TRUE]); foreach (Generate::createPaymentLineItems() as $line_item) { $this->user->get($field_name)->appendItem(['plugin_id' => $line_item->getPluginId(), 'plugin_configuration' => $line_item->getConfiguration()]); } $this->user->save(); // Create a payment method. $this->paymentMethod = Generate::createPaymentMethodConfiguration(2, 'payment_basic'); $this->paymentMethod->setPluginConfiguration(['execute_status_id' => $this->executeStatusPluginId]); $this->paymentMethod->save(); }