You are here

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

Return the not available content types.

Return value

array An array with the not available content types machine name.

Overrides OnlyOneInterface::getNotAvailableContentTypes

File

src/OnlyOne.php, line 225

Class

OnlyOne
Class OnlyOne.

Namespace

Drupal\onlyone

Code

public function getNotAvailableContentTypes() {

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

    // Looking for content types with more than 1 node
    // in at least one language.
    $query = 'SELECT DISTINCT node.type
                FROM {node} node
                JOIN {node_field_data} node_field_data USING(nid)
                GROUP BY type,
                         node_field_data.langcode
                HAVING COUNT(nid) > 1
                ORDER BY node.type ASC';
  }
  else {

    // If the site is not multilingual we have only one language,
    // and if we have a content type with more than one node,
    // then it is not available for the Only One feature.
    // Searching all the content types with nodes.
    $query = "SELECT type\n                FROM {node}\n                GROUP BY type\n                HAVING COUNT(nid) > 1\n                ORDER BY type ASC";
  }

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

  // Getting the content types machine names.
  $content_types = $result
    ->fetchCol();
  return $content_types;
}