You are here

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

Return the available content types.

Return value

array An array with the available content types machine name.

Overrides OnlyOneInterface::getAvailableContentTypes

File

src/OnlyOne.php, line 187

Class

OnlyOne
Class OnlyOne.

Namespace

Drupal\onlyone

Code

public function getAvailableContentTypes() {

  // 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 type\n                FROM {$content_types_temporary_table_name}\n                WHERE 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                ORDER BY type ASC";
  }
  else {

    // If the site is not multilingual we have only one language.
    $query = "SELECT node_type.type\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 content types machine names.
  $content_types = $result
    ->fetchCol();
  return $content_types;
}