You are here

public function OnlyOne::getAvailableContentTypesSummarized in Allow a content type only once (Only One) 8

Return the available content types with their number of nodes.

Return value

array An array of objects with the available content types keyed by content type machine name.

Overrides OnlyOneInterface::getAvailableContentTypesSummarized

1 call to OnlyOne::getAvailableContentTypesSummarized()
OnlyOne::getAvailableContentTypesForPrint in src/OnlyOne.php
Return a list of available content types for print.

File

src/OnlyOne.php, line 260

Class

OnlyOne
Class OnlyOne.

Namespace

Drupal\onlyone

Code

public function getAvailableContentTypesSummarized() {

  // Getting the temporary table with all the content type names.
  $content_types_temporary_table_name = $this
    ->getTemporaryContentTypesTableName();

  // Multilingual site?
  if ($this->languageManager
    ->isMultilingual()) {

    // Looking for content types that doesn't have more than 1 node
    // in any language.
    $query = "SELECT DISTINCT node_type.type,\n                                node_field_data.langcode AS language,\n                                COUNT(node.nid) AS total\n                FROM {$content_types_temporary_table_name} node_type\n                LEFT JOIN {node} node USING(type)\n                LEFT JOIN {node_field_data} node_field_data USING(nid)\n                WHERE node_type.type NOT IN\n                    (SELECT node.type\n                     FROM {node} node\n                     JOIN {node_field_data} node_field_data USING(nid)\n                     GROUP BY type,\n                              node_field_data.langcode\n                     HAVING COUNT(nid) > 1)\n                GROUP BY type,\n                         node_field_data.langcode\n                ORDER BY node_type.type ASC";
  }
  else {

    // If the site is not multilingual we have only one language.
    $query = "SELECT type,\n                       COUNT(nid) AS total\n                FROM {$content_types_temporary_table_name} node_type\n                LEFT JOIN {node} USING(type)\n                GROUP BY type\n                HAVING COUNT(nid) <= 1\n                ORDER BY type ASC";
  }

  // Executing the query.
  $result = $this->connection
    ->query($query);

  // Getting the information keyed by content type machine name.
  $content_types = $result
    ->fetchAll(\PDO::FETCH_GROUP);

  // Adding content type name and other information.
  $this
    ->addAditionalInfoToContentTypes($content_types);

  // Sorting by content type name (label).
  uasort($content_types, function ($a, $b) {
    return $a[0]->name <=> $b[0]->name;
  });
  return $content_types;
}