/**
  * {@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;
 }
 /**
  * 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));
 }
Exemplo n.º 3
0
 /**
  * #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;
 }
 /**
  * #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;
 }
Exemplo n.º 5
0
  /**
   * {@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;
  }
 /**
  * {@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;
 }
Exemplo n.º 7
0
 /**
  * {@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);
 }
Exemplo n.º 8
0
 /**
  * #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;
 }
Exemplo n.º 10
0
 /**
  * {@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');
 }
Exemplo n.º 11
0
 /**
  * {@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;
 }
Exemplo n.º 12
0
 /**
  * #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);
 }