You are here

function _onlyone_process in Allow a content type only once (Only One) 7

Process the output.

Parameters

string $type: The site type (multilingual or not-multilingual).

string $query: A query string to obtain the information about the content types.

bool $format: (optional) A boolean indicating if we need to format the output.

Return value

array An associative array with the content type machine name as key and his status for the Only One module as value.

2 calls to _onlyone_process()
_onlyone_available_content_types in ./onlyone.helpers.inc
Return the available content types list.
_onlyone_not_available_content_types in ./onlyone.helpers.inc
Return the not available content types list.

File

./onlyone.helpers.inc, line 131
Helper functions.

Code

function _onlyone_process($type, $query, $format) {
  $array = [];

  // Executing the query.
  $result = db_query($query);

  // If we have results.
  if ($result
    ->rowCount()) {
    $initial_format = $format ? ONLYONE_FORMAT_INITIAL_ADMIN : ONLYONE_FORMAT_INITIAL_DRUSH;
    $final_format = $format ? ONLYONE_FORMAT_FINAL_ADMIN : ONLYONE_FORMAT_FINAL_DRUSH;
    if (!$format) {

      // Getting all the configured content types.
      $onlyone_content_types = variable_get('onlyone_node_types');
    }

    // Processing each content type information.
    foreach ($result as $content_type) {

      // Variable to show if the content type is configured.
      $configured = '';
      if ($format) {
        $name = $content_type->name;
      }
      else {

        // Adding machine name if the output is for a drush command.
        $name = $content_type->name . ' [' . $content_type->type . ']';

        // If the content type is configured we will aditional info in green,
        // see https://drupal.stackexchange.com/q/220229/28275 .
        if (in_array($content_type->type, $onlyone_content_types)) {
          $configured = sprintf(ONLYONE_GREEN_OUTPUT, t('Configured'));
        }
      }

      // Multilingual or not?
      switch ($type) {
        case 'multilingual':

          // Searching all the info about the nodes of each content types.
          $query = 'SELECT type,
                           language,
                           COUNT(nid) as total
                    FROM {node_type} LEFT JOIN {node} USING(type)
                    WHERE type = :type
                    GROUP BY type,
                             language
                    ORDER BY language ASC';
          $result_info = db_query($query, [
            ':type' => $content_type->type,
          ]);

          // Variable to store the quantity of nodes by language.
          $cant_by_language = [];

          // Procesing each language.
          foreach ($result_info as $info) {

            // If we have nodes we need to add the language.
            if ($info->total) {

              // If the language is empty then is Undetermined.
              $info->language = $info->language == LANGUAGE_NONE || empty($info->language) ? t('Language neutral') : ucfirst($info->language);
              $cant_by_language[] = format_plural($info->total, '@language: @total Node', '@language: @total Nodes', [
                '@language' => $info->language,
                '@total' => $info->total,
              ]);
            }
            else {

              // If we don't have nodes we don't need the language.
              $cant_by_language[] = t('@total Nodes', [
                '@total' => 0,
              ]);
            }
          }

          // Example: Article (En: 7 Nodes, Fr: 5 Nodes, Undetermined: 2 Nodes)
          $value = $name . $initial_format . implode(', ', $cant_by_language) . $final_format . ' ' . $configured;
          $array[$info->type] = $value;
          break;
        case 'not-multilingual':

          // Example: Article (7 Nodes)
          $cant = format_plural($content_type->total, '@total Node', '@total Nodes', [
            '@total' => $content_type->total,
          ]);
          $value = $name . $initial_format . $cant . $final_format . ' ' . $configured;
          $array[$content_type->type] = $value;
          break;
      }
    }
    asort($array, SORT_NATURAL | SORT_FLAG_CASE);
  }
  return $array;
}