1

I have created 2 blocks from my custom module; those blocks mainly collect and show data from the recent content of sub-domain.
So, in these blocks I have pulled data using db_query() from another database using db_set_active('another_db').

All works fine.

My problem is that those blocks show to logged-in users only, and I want to show those blocks for all users.

I can't figure it out.

This is the code I am using.

function myblocks_block_info() {
  $blocks['latest_products'] = array(
    'info' => t('Latest Product'),
    'cache' => DRUPAL_NO_CACHE,
  );

  return $blocks;
}

function myblocks_theme() {
  return array(
    'grid_list_view' => array(
      'variables' => array('items' => NULL),
      'template'  => 'grid_list_view',
    )
  );
}

function myblocks_block_view($delta = '') {
  $block = array();

  switch ($delta) {
    case 'latest_products':
      // Create your block content here
      $block['subject'] = t('Latest Products');

      $block_categories_data = "";

      // Connect Electronics Database.
        db_set_active('another_db');
        $block_terms_id = array('76','83','42');

        for ($i=0; $i<count($block_terms_id); $i++) {
          $sql = 'SELECT DISTINCT node_taxonomy_term_data.nid AS node_taxonomy_term_data_nid
                    FROM
                    {taxonomy_term_data} taxonomy_term_data
                    LEFT JOIN {node} node_taxonomy_term_data ON (SELECT nodeINNER.nid AS nidINNER
                    FROM
                    {node} nodeINNER
                    LEFT JOIN {taxonomy_index} taxonomy_indexINNER ON nodeINNER.nid = taxonomy_indexINNER.nid
                    LEFT JOIN {taxonomy_term_data} taxonomy_term_data_nodeINNER ON taxonomy_indexINNER.tid = taxonomy_term_data_nodeINNER.tid
                    WHERE (( (taxonomy_term_data_nodeINNER.tid = taxonomy_term_data.tid ) ))
                    ORDER BY nodeINNER.nid DESC
                    LIMIT 1 OFFSET 0) = node_taxonomy_term_data.nid
                    INNER JOIN {field_data_field_pks_category} node_taxonomy_term_data__field_data_field_pks_category ON node_taxonomy_term_data.nid = node_taxonomy_term_data__field_data_field_pks_category.entity_id AND node_taxonomy_term_data__field_data_field_pks_category.entity_type = \'node\'
                    INNER JOIN {field_data_field_pks_sold_out} node_taxonomy_term_data__field_data_field_pks_sold_out ON node_taxonomy_term_data.nid = node_taxonomy_term_data__field_data_field_pks_sold_out.entity_id AND node_taxonomy_term_data__field_data_field_pks_sold_out.entity_type = \'node\'
                    WHERE (( (node_taxonomy_term_data.type IN  (\'pk_service\')) AND (node_taxonomy_term_data__field_data_field_pks_category.field_pks_category_tid = '. $block_terms_id[$i] .') AND (node_taxonomy_term_data.status = \'1\') AND (node_taxonomy_term_data__field_data_field_pks_sold_out.field_pks_sold_out_value = \'0\') ))
                    LIMIT 3 OFFSET 0';

            $result = db_query($sql);

            $rows = array();

            foreach ($result as $data) {
              // Collect All Fields Data from NID.
              $node = node_load($data->node_taxonomy_term_data_nid);
              $image_name = $node->field_pks_image['und'][0]['filename'];
              $path = drupal_get_path_alias('node/'. $node->nid);
              $category_name = taxonomy_term_load($node->field_pks_category['und'][0]['tid']);
              $category_path = strtolower(preg_replace('/[^a-zA-Z0-9-]+/', '-', $category_name->name));

              $rows[] = array(
                'title' => $node->title,
                'category_path' => $category_path,
                'category_name' => $category_name->name,
                'image_name' => $image_name,
                'path' => $path
              );
            }

            // Block Content.
            if (!empty($rows)) {
              $block_categories_data .= theme('grid_list_view', array('rows' => $rows));
            }
            else {
             // dpm($rows);
            }
        }

        // Block Content.
        $block['content'] = $block_categories_data;

        // Main DB Connect Again.
        db_set_active();
        break;
    }

    return $block;
}
  • Welcome to Drupal Answers! Since the question is about code you wrote, we need to see it. Otherwise, we cannot reply to why your code doesn't show the blocks to anonymous users. – kiamlaluno Feb 4 '17 at 15:48
  • @kiamlaluno I update my ques. You can review it please. – Tazim Feb 4 '17 at 17:58
  • I don't see anything in your code that would prevent it from not being shown to users based on role. Have you checked the settings for the block? Are they maybe restricted by role? – Jaypan Feb 5 '17 at 2:19
  • Nope :( I checked everything... But nothing goes working... – Tazim Feb 5 '17 at 12:16
0

Replace this theme

$block_categories_data .= theme('grid_list_view', array('rows' => $rows));

with add direct html code here, like -

$block_categories_data .= '<div>my html element goes here.</div>';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.