signalpoint / DrupalGap

An application development kit for Drupal websites.
https://www.drupalgap.org
GNU General Public License v2.0
232 stars 185 forks source link

visibility rules for blocks #987

Closed yazzou closed 6 years ago

yazzou commented 6 years ago

Hello I have created a banner block that displays json view. Everything is pulled up correctly. I am however only able to display the block either on front page or on nodes but not on both at the same time. The visibility rule that only display the block on front page is the following :

  sub_header: {

    title: { },
      _prefix: {
    simpleads_block: {
        pages: {
            value: ['front', 'node/*'],
            mode: 'include'
                },

          }
                }
  },

If i remove the front, it displays correctly on the nodes, but not on the front...

signalpoint commented 6 years ago

@yazzou When you have a single rule like front or node/* the block visibility rules work very well, but once you start mixing and matching, it doesn't work so well in DG7. That's why I added the Custom access_callback function section on this page: http://docs.drupalgap.org/7/Blocks/Block_Visibility_Rules

It allows you to make programmatic decisions about when to display a block, I absolutely recommend this approach instead.

yazzou commented 6 years ago

Here is my function :

function simpleads_access_callback(options) {

 //console.log(options); // Un-comment to reveal info about the current context.

 /*
  if (options.path == 'front') {
    return true;
  }
  return false;*/
 if (options.path == 'node/*') {
    return true;
  }
  return false;
}

The 'node/*' does not have any effect. It does not display the banner. However, when i use front instead it works

signalpoint commented 6 years ago

node/% is a route, not a path, so it'll never match the path. This is what you're looking for:

if (drupalgap_router_path_get() == 'node/%') {
  return true;
}

Notice it uses a %, not a *.

yazzou commented 6 years ago

This works for displaying the banner on the content :

function simpleads_access_callback(options) {

// console.log(options); // Un-comment to reveal info about the current context.

  if (drupalgap_router_path_get() == 'node/%' ) {
  return true;
}
  return false;

}

I have tried to diplay on both content and front, but it only displays on the front :

function simpleads_access_callback(options) {

// console.log(options); // Un-comment to reveal info about the current context.

  if (drupalgap_router_path_get() == 'node/%' || drupalgap_router_path_get() == 'front' ) {
  return true;
}
  return false;

}
signalpoint commented 6 years ago

Use this:

if (options.path == 'front' || drupalgap_router_path_get() == 'node/%') {
  return true;
}
return false;
yazzou commented 6 years ago

Well I tried it this morning too and retried now, the banner is displayed only on front. I have found an old post (https://www.drupal.org/node/2413509#comment-9549277) , maybe i have the same problem. Not sure thought because it is 3 years old already. Maybe it is because my block contains a view.

signalpoint commented 6 years ago

What's your View Render Array code look like in your block? You need to add a node id to the View's id attribute so it can render on every page.

yazzou commented 6 years ago

Here is my view render array:


{
  "nodes" : [
    {
      "node" : {
        "field_ad_category" : "Zone A",
        "field_ad_image" : {
          "src" : "https://www.mysite.com/sites/www.mysite.com/files/styles/bootstrap3_col12/public/field/image/photo.jpg?itok=-XrOzHlM",
          "alt" : ""
        }
      }
    }
  ],
  "view" : {
    "name" : "drupalgap_banieres_zone_a",
    "display" : "drupalgap_page_sponso_a",
    "path" : "admin/structure/views/nojs/preview/drupalgap_banieres_zone_a/drupalgap_page_sponso_a",
    "root" : "nodes",
    "child" : "node",
    "pages" : null,
    "page" : null,
    "count" : 1,
    "limit" : null
  }
}
signalpoint commented 6 years ago

I'm referring to the chunk of code in e.g. my_module_articles_page() on http://docs.drupalgap.org/7/Views/Displaying_a_View/Views_Render_Array, what does your path value look like there? And more importantly, what does its attribute/id look like?

yazzou commented 6 years ago

Here is the my module code

/**
/**
 * Implements hook_block_info().
 */
function simpleads_block_info() {
    try{
  var blocks = {};
  blocks['simpleads_block'] = {
    delta: 'simpleads_block',
    module: 'simpleads'
  };
  return blocks;
}
 catch (error) { console.log('simpleads_block_info - ' + error); }
}

/**
 * Implements hook_block_view().
 */
function simpleads_block_view(delta, region) {
  try {
  var content = {};
  if (delta == 'simpleads_block') {

    content['simpleads_list'] = {
      theme: 'view',
      format: 'none',
      path: 'sponso-zone-a', /* the path to the view in Drupal */
      row_callback: 'simpleads_list_row',
      empty_callback: 'simpleads_list_empty',
      attributes: {
        id: 'drupalgap_page_sponso_a'
      }
    };

  }
  return content;
}
catch (error) { console.log('simpleads - ' + error); }
}

/**
 * The row callback to render a single row.
 */
function simpleads_list_row(view, row, variables) {
  try {

    //var image_html = theme('image', { path: row.Image.src});

    //return l(image_html + t(row.title), 'node/' + row.nid);

    //return l(image_html + t(row.title), 'node/' + row.nid);
    var image_html = theme('image', { path: row.field_ad_image.src});

    return l(image_html);
        //var block_id = '#sponsor_block' + min_page_id;
        //var html = '<p id="sponsor_block' + min_page_id +'">Loading...</p>'; 
        //$(block_id).html(items).trigger('create');

    //return l(t(row.field_ad_image));

  }
  catch (error) { console.log('simpleads_list_row - ' + error); }
}
signalpoint commented 6 years ago

Since DG7 is a multi page app, as you navigate around your pages remain in the DOM, so your ID's need to be unique enough to move along with the pages. Change the id on your view's attributes to be dynamic.

      attributes: {
        id: 'drupalgap_page_sponso_a_' + nid
      }
yazzou commented 6 years ago

The view shows up only one banner at time, so i tried this and it seems to work

  attributes: {
       id: 'drupalgap_page_sponso_a_' + Math.floor((Math.random() * 100) + 1)
      }