0

I am needing to do a Views report for all webforms on our site (we currently have 27 forms) that can show me the total submission count per form.

The issue I am having is that we need the Views report to also show the Administrative Description of the form but I do not see that as a field option in Views.

Any ideas on how I can achieve this?

2 Answers 2

0

Ok here is one way to do it does however involve creating a custom twig Extension. its not to hard and there are a few youtube videos on the subject ... for others it is good to note that custom Twig Extensions has replaced php input fields in older versions of Drupal.

in a nut shell you create a custom module, you add a MODULENAME.services.yml file.

in it you put...

services:
modulename.getWebformDescription:
  class: Drupal\modulename\getWebformDescriptionTwigExtension
  arguments: ['@entity_type.manager']
  tags:
    - { name: twig.extension }

then you need here is your class file (getWebformDescriptionTwigExtension.php)

<?php

namespace Drupal\modulename;

use Drupal\Core\Entity\EntityTypeManagerInterface;

/**
 * Provides twig function getWebformDescription()
 */
class getWebformDescriptionTwigExtension extends \Twig_Extension {

  /**
   * Load entity type manager.
   *
   * @var EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * {@inheritdoc}
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager) {
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * {@inheritdoc}
   */
  public function getName() {
    return 'getWebformDescription';
  }

  /**
   * {@inheritdoc}
   */
  public function getFunctions() {
    return [
      new \Twig_SimpleFunction('getWebformDescription',
        [$this, 'getWebformDescription'], [
          'is_safe' => ['html'],
        ]),
    ];
  }

  /**
   * Provides getWebformDescription function to Twig templates.
   *
   * Use example {{ getWebformDescription( webfrom_id) }}.
   *
   * @param string webfrom_id
   *   The webform id eg 'contact'
   */
  public function getWebformDescription($webfrom_id) : string {
    // Invoke __toString and cast to int.
    $webfrom_id = $webfrom_id->__toString();
    $webform_storage = $this->entityTypeManager->getStorage('webform');
    $webform = $webform_storage->load($webfrom_id);
    return $webform->get('description');
  }

}

after all that is done and the module is enabled you can then in views add the webform id field (and exclude it from display)

then add a new custom field and look at the replacement patterns you will see some thing like {{webfrom_id}} ...

in that field you can then enter ...

 {{ getWebformDescription(webfrom_id) }}

if stuck see stuff like https://medium.com/@thihathit/extending-custom-twig-extension-to-drupal-8-twig-extension-class-d4b99c2177ae

or google "create custom twig extension for drupal 8"

0

Thank you taggartJ

I feel like this question hasn't helped many people over the time since it's very specific, but it helped me.

I'm trying to do something similar in a Drupal 9 custom module:

function MY_MODULE_views_pre_render(ViewExecutable $view) {
  $view_id = $view->id();
  $view_title = $view->display_handler->getOption('title');
  $view_description = $view->storage->get('description');
  ...
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.