/** * {@inheritdoc} */ public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) { $config = $this->configFactory->get('payment_reference.payment_type'); $payment = $this->paymentFactory->createPayment($this->fieldDefinition); $element['target_id'] = array('#default_value' => isset($items[$delta]) ? (int) $items[$delta]->target_id : NULL, '#limit_allowed_plugin_ids' => $config->get('limit_allowed_plugins') ? $config->get('allowed_plugin_ids') : NULL, '#plugin_selector_id' => $config->get('plugin_selector_id'), '#prototype_payment' => $payment, '#queue_category_id' => $items->getEntity()->getEntityTypeId() . '.' . $items->getEntity()->bundle() . '.' . $this->fieldDefinition->getName(), '#queue_owner_id' => (int) $this->currentUser->id(), '#required' => $this->fieldDefinition->isRequired(), '#type' => 'payment_reference'); return $element; }
/** * @covers ::formElement */ public function testFormElement() { $entity_type_id = $this->randomMachineName(); $bundle = $this->randomMachineName(); $field_name = $this->randomMachineName(); $user_id = mt_rand(); $required = TRUE; $entity = $this->getMock(EntityInterface::class); $entity->expects($this->atLeastOnce())->method('bundle')->willReturn($bundle); $entity->expects($this->atLeastOnce())->method('getEntityTypeId')->willReturn($entity_type_id); $this->fieldDefinition->expects($this->once())->method('getName')->willReturn($field_name); $this->fieldDefinition->expects($this->once())->method('isRequired')->willReturn($required); $payment = $this->getMock(PaymentInterface::class); $this->paymentFactory->expects($this->once())->method('createPayment')->with($this->fieldDefinition)->willReturn($payment); $this->currentUser->expects($this->exactly(1))->method('id')->willReturn($user_id); $items = $this->getMockBuilder(FieldItemList::class)->disableOriginalConstructor()->getMock(); $items->expects($this->atLeastOnce())->method('getEntity')->willReturn($entity); $form = []; $form_state = $this->getMock(FormStateInterface::class); $build = $this->sut->formElement($items, 3, [], $form, $form_state); $expected_build = array('target_id' => array('#default_value' => NULL, '#limit_allowed_plugin_ids' => $this->configFactoryConfiguration['payment_reference.payment_type']['allowed_plugin_ids'], '#plugin_selector_id' => $this->configFactoryConfiguration['payment_reference.payment_type']['plugin_selector_id'], '#prototype_payment' => $payment, '#queue_category_id' => $entity_type_id . '.' . $bundle . '.' . $field_name, '#queue_owner_id' => (int) $user_id, '#required' => $required, '#type' => 'payment_reference')); $this->assertSame($expected_build, $build); }