You are here

public function GISController::googleImageSitemapBuild in Google Image Sitemap 8

Same name and namespace in other branches
  1. 2.0.x src/Controller/GISController.php \Drupal\google_image_sitemap\Controller\GISController::googleImageSitemapBuild()
  2. 1.0.x src/Controller/GISController.php \Drupal\google_image_sitemap\Controller\GISController::googleImageSitemapBuild()

Function to generate image sitemap.

1 string reference to 'GISController::googleImageSitemapBuild'
google_image_sitemap.routing.yml in ./google_image_sitemap.routing.yml
google_image_sitemap.routing.yml

File

src/Controller/GISController.php, line 99

Class

GISController
Default controller for the google_image_sitemap module.

Namespace

Drupal\google_image_sitemap\Controller

Code

public function googleImageSitemapBuild($sitemap_id) {
  $query = $this->db
    ->select('google_image_sitemap', 'g')
    ->fields('g')
    ->condition('sid', $sitemap_id);
  $result = $query
    ->execute()
    ->fetchObject();
  $filename = 'google_image_sitemap.xml';
  if (!empty($result)) {
    $query = $this->db
      ->select('node_field_data', 'nfd');
    $query
      ->fields('nfd', [
      'nid',
      'title',
    ]);
    $query
      ->fields('f', [
      'uri',
    ]);
    $query
      ->innerJoin('file_usage', 'fu', "nfd.nid = fu.id");
    $query
      ->innerJoin('file_managed', 'f', "fu.fid = f.fid");
    $query
      ->condition('f.filemime', [
      'image/png',
      'image/jpg',
      'image/gif',
      'image/jpeg',
    ], 'IN');
    if ($result->node_type != 'all') {
      $query
        ->condition('nfd.type', $result->node_type);
      $filename = 'google_image_sitemap_' . $result->node_type . '.xml';
    }
    $query
      ->orderBy('nfd.nid', 'DESC');
    $nodes = $query
      ->execute()
      ->fetchAll();
    if (!empty($nodes)) {
      $output = '<?xml version="1.0" encoding="UTF-8"?>';
      $output .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
                  xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">';
      foreach ($nodes as $node) {
        $output .= '<url><loc>' . Url::fromUri('internal:/node/' . $node->nid, [
          'absolute' => TRUE,
        ])
          ->toString() . '</loc>
                     <image:image>
                       <image:loc>' . file_create_url($node->uri) . '</image:loc>
                       <image:title>' . $node->title . '</image:title>
                       <image:caption>' . $node->title . '</image:caption>
                       <image:license>' . $result->license . '</image:license>
                     </image:image></url>';
      }
      $output .= '</urlset>';

      // File build path.
      $path = \Drupal::service('file_system')
        ->realpath(file_default_scheme() . "://") . '/google_image_sitemap';
      if (!is_dir($path)) {
        \Drupal::service('file_system')
          ->mkdir($path);
      }
      if ($file = file_unmanaged_save_data($output, $path . '/' . $filename, FILE_EXISTS_REPLACE)) {
        $this->db
          ->update('google_image_sitemap')
          ->fields([
          'last_updated' => \Drupal::time()
            ->getRequestTime(),
        ])
          ->condition('sid', $sitemap_id, '=')
          ->execute();
        drupal_set_message($this
          ->t("Sitemap created successfully!"));
      }
    }
    else {
      drupal_set_message($this
        ->t("No Images found!"));
    }
    global $base_url;
    $redirect = new RedirectResponse($base_url . '/' . GISController::GOOGLE_IMAGE_SITEMAP_ADMIN_PATH);
    $redirect
      ->send();
  }
}