/** * @covers ::execute */ public function testExecute() { $payment_status = $this->getMock(PaymentStatusInterface::class); $this->paymentStatusStorage->expects($this->once())->method('create')->willReturn($payment_status); $form = $this->getMock(FormInterface::class); $this->entityFormBuilder->expects($this->once())->method('getForm')->with($payment_status)->willReturn($form); $this->assertSame($form, $this->sut->execute()); }
/** * @covers ::execute */ public function testExecute() { $currency = $this->getMock(CurrencyInterface::class); $this->currencyStorage->expects($this->once())->method('create')->with(array())->willReturn($currency); $form = $this->getMock(EntityFormInterface::class); $this->entityFormBuilder->expects($this->once())->method('getForm')->with($currency)->willReturn($form); $this->assertSame($form, $this->sut->execute()); }
/** * {@inheritdoc} */ public function view(EntityInterface $entity, $view_mode = 'full', $langcode = NULL) { $message = $this->contactMessageStorage->create(['contact_form' => $entity->id()]); $form = $this->entityFormBuilder->getForm($message); $form['#title'] = $entity->label(); $form['#cache']['contexts'][] = 'user.permissions'; $this->renderer->addCacheableDependency($form, $this->config); return $form; }
/** * @covers ::execute */ public function testExecute() { $payment_method_configuration = $this->getMock(PaymentMethodConfigurationInterface::class); $payment_method_configuration->expects($this->once())->method('createDuplicate')->willReturnSelf(); $payment_method_configuration->expects($this->once())->method('setLabel')->willReturnSelf(); $form = $this->getMock(EntityFormInterface::class); $this->entityFormBuilder->expects($this->once())->method('getForm')->with($payment_method_configuration, 'default')->willReturn($form); $this->sut->execute($payment_method_configuration); }
/** * Tests the getForm() method. * * @covers ::getForm() */ public function testGetForm() { $form_controller = $this->getMock('Drupal\\Core\\Entity\\EntityFormInterface'); $form_controller->expects($this->any())->method('getFormId')->will($this->returnValue('the_form_id')); $this->entityManager->expects($this->any())->method('getFormObject')->with('the_entity_type', 'default')->will($this->returnValue($form_controller)); $this->formBuilder->expects($this->once())->method('buildForm')->with($form_controller, $this->isInstanceOf('Drupal\\Core\\Form\\FormStateInterface'))->will($this->returnValue('the form contents')); $entity = $this->getMock('Drupal\\Core\\Entity\\EntityInterface'); $entity->expects($this->once())->method('getEntityTypeId')->will($this->returnValue('the_entity_type')); $this->assertSame('the form contents', $this->entityFormBuilder->getForm($entity)); }
/** * #post_render_cache callback; replaces placeholder with comment form. * * @param array $element * The renderable array that contains the to be replaced placeholder. * @param array $context * An array with the following keys: * - entity_type: an entity type * - entity_id: an entity ID * - field_name: a comment field name * * @return array * A renderable array containing the comment form. */ public function renderForm(array $element, array $context) { $field_name = $context['field_name']; $entity = $this->entityManager->getStorage($context['entity_type'])->load($context['entity_id']); $field_storage = FieldStorageConfig::loadByName($entity->getEntityTypeId(), $field_name); $values = array('entity_type' => $entity->getEntityTypeId(), 'entity_id' => $entity->id(), 'field_name' => $field_name, 'comment_type' => $field_storage->getSetting('bundle'), 'pid' => NULL); $comment = $this->entityManager->getStorage('comment')->create($values); $form = $this->entityFormBuilder->getForm($comment); $markup = drupal_render($form); $callback = 'comment.post_render_cache:renderForm'; $placeholder = drupal_render_cache_generate_placeholder($callback, $context); $element['#markup'] = str_replace($placeholder, $markup, $element['#markup']); $element['#attached'] = drupal_merge_attached($element['#attached'], $form['#attached']); return $element; }
/** * {@inheritdoc} */ public function view(EntityInterface $entity, $view_mode = 'full', $langcode = NULL) { if ($entity->status()) { $message = $this->contactMessageStorage->create([ 'contact_form' => $entity->id(), ]); $form = $this->entityFormBuilder->getForm($message); $form['#title'] = $entity->label(); $form['#cache']['contexts'][] = 'user.permissions'; $this->renderer->addCacheableDependency($form, $this->config); } else { // Form disabled, display a custom message using a template. $form['disabled_form_error'] = array( '#theme' => 'contact_storage_disabled_form', '#contact_form' => $entity, '#redirect_uri' => $entity->getThirdPartySetting('contact_storage', 'redirect_uri', ''), '#disabled_form_message' => $entity->getThirdPartySetting('contact_storage', 'disabled_form_message', t('This contact form has been disabled.')), ); } // Add required cacheability metadata from the contact form entity, so that // changing it invalidates the cache. $this->renderer->addCacheableDependency($form, $entity); return $form; }
/** * #post_render_cache callback; replaces placeholder with comment form. * * @param array $element * The renderable array that contains the to be replaced placeholder. * @param array $context * An array with the following keys: * - entity_type: an entity type * - entity_id: an entity ID * - field_name: a comment field name * * @return array * A renderable array containing the comment form. */ public function renderForm(array $element, array $context) { $field_name = $context['field_name']; $entity = $this->entityManager->getStorage($context['entity_type'])->load($context['entity_id']); $field_storage = FieldStorageConfig::loadByName($entity->getEntityTypeId(), $field_name); $values = array('entity_type' => $entity->getEntityTypeId(), 'entity_id' => $entity->id(), 'field_name' => $field_name, 'comment_type' => $field_storage->getSetting('bundle'), 'pid' => NULL); $comment = $this->entityManager->getStorage('comment')->create($values); $form = $this->entityFormBuilder->getForm($comment); // @todo: This only works as long as assets are still tracked in a global // static variable, see https://drupal.org/node/2238835 $markup = drupal_render($form, TRUE); $callback = 'comment.post_render_cache:renderForm'; $placeholder = drupal_render_cache_generate_placeholder($callback, $context); $element['#markup'] = str_replace($placeholder, $markup, $element['#markup']); return $element; }
/** * {@inheritdoc} */ public function viewElements(FieldItemListInterface $items) { $elements = array(); $output = array(); $field_name = $this->fieldDefinition->getName(); $entity = $items->getEntity(); $status = $items->status; if ($status != CommentItemInterface::HIDDEN && empty($entity->in_preview) && !in_array($this->viewMode, array('search_result', 'search_index'))) { $comment_settings = $this->getFieldSettings(); // Only attempt to render comments if the entity has visible comments. // Unpublished comments are not included in // $entity->get($field_name)->comment_count, but unpublished comments // should display if the user is an administrator. $elements['#cache']['contexts'][] = 'user.roles'; if ($this->currentUser->hasPermission('access comments') || $this->currentUser->hasPermission('administer comments')) { // This is a listing of Comment entities, so associate its list cache // tag for correct invalidation. $output['comments']['#cache']['tags'] = $this->entityManager->getDefinition('comment')->getListCacheTags(); if ($entity->get($field_name)->comment_count || $this->currentUser->hasPermission('administer comments')) { $mode = $comment_settings['default_mode']; $comments_per_page = $comment_settings['per_page']; $comments = $this->storage->loadThread($entity, $field_name, $mode, $comments_per_page, $this->getSetting('pager_id')); if ($comments) { comment_prepare_thread($comments); $build = $this->viewBuilder->viewMultiple($comments); $build['pager']['#type'] = 'pager'; if ($this->getSetting('pager_id')) { $build['pager']['#element'] = $this->getSetting('pager_id'); } $output['comments'] += $build; } } } // Append comment form if the comments are open and the form is set to // display below the entity. Do not show the form for the print view mode. if ($status == CommentItemInterface::OPEN && $comment_settings['form_location'] == CommentItemInterface::FORM_BELOW && $this->viewMode != 'print') { // Only show the add comment form if the user has permission. $elements['#cache']['contexts'][] = 'user.roles'; if ($this->currentUser->hasPermission('post comments')) { // All users in the "anonymous" role can use the same form: it is fine // for this form to be stored in the render cache. if ($this->currentUser->isAnonymous()) { $comment = $this->storage->create(array('entity_type' => $entity->getEntityTypeId(), 'entity_id' => $entity->id(), 'field_name' => $field_name, 'comment_type' => $this->getFieldSetting('comment_type'), 'pid' => NULL)); $output['comment_form'] = $this->entityFormBuilder->getForm($comment); } else { $callback = 'comment.post_render_cache:renderForm'; $context = array('entity_type' => $entity->getEntityTypeId(), 'entity_id' => $entity->id(), 'field_name' => $field_name); $placeholder = drupal_render_cache_generate_placeholder($callback, $context); $output['comment_form'] = array('#post_render_cache' => array($callback => array($context)), '#markup' => $placeholder); } } } $elements[] = $output + array('#comment_type' => $this->getFieldSetting('comment_type'), '#comment_display_mode' => $this->getFieldSetting('default_mode'), 'comments' => array(), 'comment_form' => array()); } return $elements; }
/** * {@inheritdoc} */ public function viewElements(FieldItemListInterface $items, $langcode) { // @todo Use a lazy_builder. $product = $items->getEntity(); $line_item_type = $this->lineItemTypeMap->getLineItemTypeId($product->bundle()); $line_item_storage = $this->entityTypeManager->getStorage('commerce_line_item'); $line_item = $line_item_storage->create(['type' => $line_item_type]); $form_state_additions = ['product' => $product, 'settings' => $this->getSettings()]; return $this->entityFormBuilder->getForm($line_item, 'add_to_cart', $form_state_additions); }
/** * @covers ::execute */ public function testExecute() { $plugin_id = $this->randomMachineName(); $payment_method_configuration = $this->getMock(PaymentMethodConfigurationInterface::class); $storage_controller = $this->getMock(EntityStorageInterface::class); $storage_controller->expects($this->once())->method('create')->willReturn($payment_method_configuration); $form = $this->getMock(EntityFormInterface::class); $this->entityManager->expects($this->once())->method('getStorage')->with('payment_method_configuration')->willReturn($storage_controller); $this->entityFormBuilder->expects($this->once())->method('getForm')->with($payment_method_configuration, 'default')->willReturn($form); $this->sut->execute($plugin_id); }
/** * #post_render_cache callback; replaces placeholder with comment form. * * @param array $element * The renderable array that contains the to be replaced placeholder. * @param array $context * An array with the following keys: * - entity_type: an entity type * - entity_id: an entity ID * - field_name: a comment field name * - comment_type: the comment type * * @return array * A renderable array containing the comment form. */ public function renderForm(array $element, array $context) { $values = array('entity_type' => $context['entity_type'], 'entity_id' => $context['entity_id'], 'field_name' => $context['field_name'], 'comment_type' => $context['comment_type'], 'pid' => NULL); $comment = $this->entityManager->getStorage('comment')->create($values); $form = $this->entityFormBuilder->getForm($comment); $markup = $this->renderer->render($form); $callback = 'comment.post_render_cache:renderForm'; $placeholder = $this->generatePlaceholder($callback, $context); $element['#markup'] = str_replace($placeholder, $markup, $element['#markup']); $element = $this->renderer->mergeBubbleableMetadata($element, $form); return $element; }
/** * {@inheritdoc} */ public function viewElements(FieldItemListInterface $items, $langcode) { $elements = array(); $entity = current($this->getEntitiesToView($items, $langcode)); if ($entity) { if ($entity->id()) { // @see \Drupal\contact\Controller\ContactController::contactSitePage(). $config = $this->configFactory->get('contact.settings')->get(); $message = $this->entityManager->getStorage('contact_message')->create(array('contact_form' => $entity->id())); $form = $this->entityFormBuilder->getForm($message); $form['#title'] = $entity->label(); $form['#cache']['contexts'][] = 'user.permissions'; $this->renderer->addCacheableDependency($form, $config); $elements[] = $form; } } return $elements; }
/** * @covers ::viewElements */ public function testViewElements() { $entity_type_id = $this->randomMachineName(); $bundle = $this->randomMachineName(); $field_name = $this->randomMachineName(); $destination_url = $this->randomMachineName(); $currency_code = $this->randomMachineName(); $plugin_id = $this->randomMachineName(); $plugin_configuration = [$this->randomMachineName() => $this->randomMachineName()]; $plugin_id_property = $this->getMock(TypedDataInterface::class); $plugin_id_property->expects($this->once())->method('getValue')->willReturn($plugin_id); $plugin_configuration_property = $this->getMock(TypedDataInterface::class); $plugin_configuration_property->expects($this->once())->method('getValue')->willReturn($plugin_configuration); $map = [['plugin_id', $plugin_id_property], ['plugin_configuration', $plugin_configuration_property]]; $item = $this->getMockBuilder(PaymentFormFieldType::class)->disableOriginalConstructor()->getMock(); $item->expects($this->exactly(2))->method('get')->willReturnMap($map); $entity = $this->getMock(EntityInterface::class); $entity->expects($this->atLeastOnce())->method('bundle')->willReturn($bundle); $entity->expects($this->atLeastOnce())->method('getEntityTypeId')->willReturn($entity_type_id); $iterator = new \ArrayIterator([$item]); $items = $this->getMockBuilder(FieldItemList::class)->disableOriginalConstructor()->setMethods(['getEntity', 'getIterator'])->getMock(); $items->expects($this->atLeastOnce())->method('getEntity')->willReturn($entity); $items->expects($this->atLeastOnce())->method('getIterator')->willReturn($iterator); $this->fieldDefinition->expects($this->once())->method('getName')->willReturn($field_name); $this->fieldDefinition->expects($this->atLeastOnce())->method('getSetting')->with('currency_code')->willReturn($currency_code); $payment_type = $this->getMockBuilder(PaymentFormPaymentType::class)->disableOriginalConstructor()->getMock(); $payment_type->expects($this->once())->method('setEntityTypeId')->with($entity_type_id); $payment_type->expects($this->once())->method('setBundle')->with($bundle); $payment_type->expects($this->once())->method('setFieldName')->with($field_name); $payment_type->expects($this->once())->method('setDestinationUrl')->with($destination_url); $payment = $this->getMock(PaymentInterface::class); $payment->expects($this->once())->method('setCurrencyCode')->with($currency_code); $payment->expects($this->once())->method('getPaymentType')->willReturn($payment_type); $payment_line_item = $this->getMock(PaymentLineItemInterface::class); $this->paymentLineItemManager->expects($this->once())->method('createInstance')->with($plugin_id, $plugin_configuration)->willReturn($payment_line_item); $this->paymentStorage->expects($this->once())->method('create')->with(['bundle' => 'payment_form'])->willReturn($payment); $this->request->expects($this->atLeastOnce())->method('getUri')->willReturn($destination_url); $form = ['#foo' => $this->randomMachineName()]; $this->entityFormBuilder->expects($this->once())->method('getForm')->with($payment, 'payment_form')->willReturn($form); $this->assertSame($form, $this->sut->viewElements($items, 'en')); }
/** * {@inheritdoc} */ public function viewElements(FieldItemListInterface $items, $langcode) { $entity_type_id = $items->getEntity()->getEntityTypeId(); $bundle = $items->getEntity()->bundle(); $field_name = $this->fieldDefinition->getName(); /** @var \Drupal\payment\Entity\PaymentInterface $payment */ $payment = $this->paymentStorage->create(['bundle' => 'payment_form']); $payment->setCurrencyCode($this->fieldDefinition->getSetting('currency_code')); /** @var \Drupal\payment_form\Plugin\Payment\Type\PaymentForm $payment_type */ $payment_type = $payment->getPaymentType(); $payment_type->setDestinationUrl($this->requestStack->getCurrentRequest()->getUri()); $payment_type->setEntityTypeId($entity_type_id); $payment_type->setBundle($bundle); $payment_type->setFieldName($field_name); foreach ($items as $item) { /** @var \Drupal\payment_form\Plugin\Field\FieldType\PaymentForm $item */ $plugin_id = $item->get('plugin_id')->getValue(); if ($plugin_id) { $payment->setLineItem($this->paymentLineItemManager->createInstance($plugin_id, $item->get('plugin_configuration')->getValue())); } } return $this->entityFormBuilder->getForm($payment, 'payment_form'); }
/** * {@inheritdoc} */ public function viewElements(FieldItemListInterface $items) { $elements = array(); $output = array(); $field_name = $this->fieldDefinition->getName(); $entity = $items->getEntity(); $status = $items->status; if ($status != CommentItemInterface::HIDDEN && empty($entity->in_preview) && !in_array($this->viewMode, array('search_result', 'search_index'))) { $comment_settings = $this->getFieldSettings(); // Only attempt to render comments if the entity has visible comments. // Unpublished comments are not included in // $entity->get($field_name)->comment_count, but unpublished comments // should display if the user is an administrator. if ($entity->get($field_name)->comment_count && $this->currentUser->hasPermission('access comments') || $this->currentUser->hasPermission('administer comments')) { $mode = $comment_settings['default_mode']; $comments_per_page = $comment_settings['per_page']; $comments = $this->storage->loadThread($entity, $field_name, $mode, $comments_per_page, $this->getSetting('pager_id')); if ($comments) { comment_prepare_thread($comments); $build = $this->viewBuilder->viewMultiple($comments); $build['pager']['#theme'] = 'pager'; if ($this->getSetting('pager_id')) { $build['pager']['#element'] = $this->getSetting('pager_id'); } // The viewElements() method of entity field formatters is run // during the #pre_render phase of rendering an entity. A formatter // builds the content of the field in preparation for theming. // All entity cache tags must be available after the #pre_render phase. // This field formatter is highly exceptional: it renders *another* // entity and this referenced entity has its own #pre_render // callbacks. In order collect the cache tags associated with the // referenced entity it must be passed to drupal_render() so that its // #pre_render callbacks are invoked and its full build array is // assembled. Rendering the referenced entity in place here will allow // its cache tags to be bubbled up and included with those of the // main entity when cache tags are collected for a renderable array // in drupal_render(). drupal_render($build, TRUE); $output['comments'] = $build; } } // Append comment form if the comments are open and the form is set to // display below the entity. Do not show the form for the print view mode. if ($status == CommentItemInterface::OPEN && $comment_settings['form_location'] == COMMENT_FORM_BELOW && $this->viewMode != 'print') { // Only show the add comment form if the user has permission. if ($this->currentUser->hasPermission('post comments')) { // All users in the "anonymous" role can use the same form: it is fine // for this form to be stored in the render cache. if ($this->currentUser->isAnonymous()) { $comment = $this->storage->create(array('entity_type' => $entity->getEntityTypeId(), 'entity_id' => $entity->id(), 'field_name' => $field_name, 'comment_type' => $this->getFieldSetting('comment_type'), 'pid' => NULL)); $output['comment_form'] = $this->entityFormBuilder->getForm($comment); } else { $callback = 'comment.post_render_cache:renderForm'; $context = array('entity_type' => $entity->getEntityTypeId(), 'entity_id' => $entity->id(), 'field_name' => $field_name); $placeholder = drupal_render_cache_generate_placeholder($callback, $context); $output['comment_form'] = array('#post_render_cache' => array($callback => array($context)), '#markup' => $placeholder); } } } $elements[] = $output + array('#comment_type' => $this->getFieldSetting('comment_type'), '#comment_display_mode' => $this->getFieldSetting('default_mode'), 'comments' => array(), 'comment_form' => array()); } return $elements; }
/** * #lazy_builder callback; builds the comment form. * * @param string $commented_entity_type_id * The commented entity type ID. * @param string $commented_entity_id * The commented entity ID. * @param string $field_name * The comment field name. * @param string $comment_type_id * The comment type ID. * * @return array * A renderable array containing the comment form. */ public function renderForm($commented_entity_type_id, $commented_entity_id, $field_name, $comment_type_id) { $values = array('entity_type' => $commented_entity_type_id, 'entity_id' => $commented_entity_id, 'field_name' => $field_name, 'comment_type' => $comment_type_id, 'pid' => NULL); $comment = $this->entityManager->getStorage('comment')->create($values); return $this->entityFormBuilder->getForm($comment); }