You are here

protected function ContactEmails::getContactFormsWithEmails in Contact Emails 8

Get a list of contact forms that have emails.

Parameters

bool $from_cache: Whether to load from the cache (if available).

Return value

array An array of contact form ids that have at least 1 email.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

1 call to ContactEmails::getContactFormsWithEmails()
ContactEmails::rebuildCache in src/ContactEmails.php
Rebuild any caches.

File

src/ContactEmails.php, line 66

Class

ContactEmails
Class ContactEmails.

Namespace

Drupal\contact_emails

Code

protected function getContactFormsWithEmails($from_cache = TRUE) {
  $cid = 'contact_emails:contact_forms_with_emails';

  // Load a list of the forms with at least one contact_emails email.
  if ($from_cache && ($cache = $this->cache
    ->get($cid))) {
    $contact_forms = $cache->data;
  }
  else {

    /** @var \Drupal\contact_emails\ContactEmailStorageInterface $storage */
    $storage = \Drupal::entityTypeManager()
      ->getStorage('contact_email');
    $emails = $storage
      ->loadMultiple();
    $contact_forms = [];

    /** @var \Drupal\contact_emails\Entity\ContactEmailInterface $email */
    foreach ($emails as $email) {
      $formId = $email
        ->get('contact_form')->target_id;
      if (array_search($formId, $contact_forms) === FALSE) {
        $contact_forms[] = $formId;
      }
    }
    $this->cache
      ->set($cid, $contact_forms);
  }
  return $contact_forms;
}