ConorBaumgart
ConorBaumgart

Reputation: 513

Drupal 8 - How do I detect whether any contact forms have been created?

Context: On my Drupal 8 site I'm trying to add in a new section containing a view that lists out all forms. The user can then click on one of the form and it launches a download of all the messages sent through that form. However that section should only be visible if the user has created at least 1 form.

What I've tried: So far what I've discovered is that the contact forms are stored in the config table in the database, so I could theoretically run a query like "SELECT 1 FROM config WHERE name LIKE '%contact.form%'" or whatever the equivalent in D8 is. However, it seems like there has to be a quicker way to return either the presence of contact forms or the contact forms themselves programmatically.

Final question: How can I return either the contact forms themselves or at least a boolean representing the presence of contact forms programmatically?

Upvotes: 0

Views: 360

Answers (1)

baikho
baikho

Reputation: 5368

Querying contact form entities

\Drupal::entityTypeManager()
   ->getStorage('contact_form')
   ->loadMultiple();

=> [
  "feedback" => Drupal\contact\Entity\ContactForm {#3213},
  "personal" => Drupal\contact\Entity\ContactForm {#3994},
]

Contact forms count

\Drupal::entityTypeManager()
  ->getStorage('contact_form')
  ->getQuery()
  ->count()
  ->execute();

=> 2

And on your final question; 1 way to approach this, is to subclass ContactFormListBuilder and override its default render() logic:

Routing YAML

my_module.custom_contact_form_list:
  path: '/admin/foo/custom-contact-form-listing'
  defaults:
    _controller: '\Drupal\my_module\Controller\ContactFormListController::listing'
    _title: 'A custom contact form listing view'
  requirements:
    _permission: 'administer contact forms'

Listing Controller

<?php

namespace Drupal\my_module\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\my_module\CustomContactFormListBuilder;

/**
 * Class ContactFormListController.
 *
 * @package Drupal\my_module\Controller
 */
class ContactFormListController extends ControllerBase {

  /**
   * Provides a custom listing page for contact forms.
   *
   * @return array
   *   A render array as expected by
   *   \Drupal\Core\Render\RendererInterface::render().
   */
  public function listing() {
    $definition = $this->entityTypeManager()->getDefinition('contact_form');
    return $this
      ->entityTypeManager()
      ->createHandlerInstance(CustomContactFormListBuilder::class, $definition)
      ->render();
  }
}

List Builder class

<?php

namespace Drupal\my_module;

use Drupal\contact\ContactFormListBuilder;

/**
 * Class CustomContactFormListBuilder.
 *
 * @package Drupal\my_module
 */
class CustomContactFormListBuilder extends ContactFormListBuilder {

  /**
   * {@inheritdoc}
   */
  public function render() {
    return parent::render(); // TODO: Change the autogenerated stub
  }

}

Upvotes: 0

Related Questions