While building a custom filter for Views 2 on a Drupal 6 site I leaned an important lesson about the way Drupal handles permissions.
I was trying to write a filter that would display a selected list of nodes to users with a certain role. Sounds easy right? It would be fairly logical to assume that Drupal would support filtering access to nodes based on role. If you want to restrict access to an entire content type by role, this is easy. The 'views' explanation would be something like: "Show me nodes of this type, only to users who have this role".
My situation was different because the content type is accessible to all authenticated users, I only want to show a small subset of those nodes to users with a specific role. That subset of nodes has no distinguishing property to filter, leading me to believe that I would have to write my own.
Here is where the trouble starts.
I wanted the filter to do a join between the 'node' table and the 'node_access' table to see if a particular node id was listed in the node_access table with the role id ( 'gid' in the table ) of the role to which I wanted to display the nodes. I was surprised when my queries were coming back empty.
I noticed that nodes of this content type would be returned with my custom filter when I used a role id that had access to nodes not available to 'authenticated users'. To test this discovery I removed node access for authenticated users to the nodes I wanted available to the new role, and suddenly they were being listed in 'node_access' with the new role's id.
In summary: If a node is accessible by the role 'authenticated user', the node_access table will only contain records associated with that role id. Other roles given the same access will not have a record in the node_access table. This is most likely intended to reduce redundancy in the table because any user with any custom role will already be authenticated.
This is probably why this filter is not included in Views by default.
Unfortunately this means my query is impossible ( at least the way I attempted it ), and there goes several hours of facemashing my keyboard.
Snippets of code and tricks that would otherwise be forgotten. Useful for me, hopefully for you too.
Wednesday, January 30, 2013
Thursday, January 24, 2013
Javascript query parameter setup
Using a query parameter can often be more convenient than having to change a hardcoded value while testing a script. This allows you to pass a variable into the script without having to edit and re-save your script.
var query = window.location.search.substring(1);
var keyVal = query.split("=");
var value = keyVal[1];
if (!value) { value = 100; } // in case no query parameter is passed in, use a default
var query = window.location.search.substring(1);
var keyVal = query.split("=");
var value = keyVal[1];
if (!value) { value = 100; } // in case no query parameter is passed in, use a default
Wednesday, January 9, 2013
Assigning CSS float values in javascript
I have to give credit for this information to an anonymous poster on stack overflow: http://stackoverflow.com/questions/606470/is-there-a-cross-browser-way-of-setting-style-float-in-javascript
While creating and adding styles to some DOM elements with javascript, I noticed that my IDE was unhappy with me using this syntax:
domElement.style.float = "right";
The trouble is that 'float' is a reserved keyword in javascript, and the above syntax will cause unexpected results which vary across browsers. I learned that I should be using: domElement.style.styleFloat = "right"; but this was still not working Firefox.
The cross browser solution ( Chrome, IE, Firefox ) :
domElement.style.styleFloat = "right";
domElement.style.cssFloat = "right";
Now everyone is happy.
While creating and adding styles to some DOM elements with javascript, I noticed that my IDE was unhappy with me using this syntax:
domElement.style.float = "right";
The trouble is that 'float' is a reserved keyword in javascript, and the above syntax will cause unexpected results which vary across browsers. I learned that I should be using: domElement.style.styleFloat = "right"; but this was still not working Firefox.
The cross browser solution ( Chrome, IE, Firefox ) :
domElement.style.styleFloat = "right";
domElement.style.cssFloat = "right";
Now everyone is happy.
Tuesday, December 18, 2012
Pattern Resource
A great collection of subtle patterns for web development and graphic design:
http://subtlepatterns.com/
http://subtlepatterns.com/
Wednesday, December 12, 2012
Cross Browser Opacity Classes
These come in handy pretty often:
.no_opacity {
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)"; /* IE 8 */
filter: alpha(opacity=0); /* IE 5-7 */
-moz-opacity: 0.0; /* Netscape */
-khtml-opacity: 0.0; /* Safari 1.x */
opacity: 0.0; /* Good browsers */
}
.half_opacity {
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=50)"; /* IE 8 */
filter: alpha(opacity=50); /* IE 5-7 */
-moz-opacity: 0.5; /* Netscape */
-khtml-opacity: 0.5; /* Safari 1.x */
opacity: 0.5; /* Good browsers */
}
.full_opacity {
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=100)"; /* IE 8 */
filter: alpha(opacity=100); /* IE 5-7 */
-moz-opacity: 1.0; /* Netscape */
-khtml-opacity: 1.0; /* Safari 1.x */
opacity: 1.0; /* Good browsers */
}
.no_opacity {
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)"; /* IE 8 */
filter: alpha(opacity=0); /* IE 5-7 */
-moz-opacity: 0.0; /* Netscape */
-khtml-opacity: 0.0; /* Safari 1.x */
opacity: 0.0; /* Good browsers */
}
.half_opacity {
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=50)"; /* IE 8 */
filter: alpha(opacity=50); /* IE 5-7 */
-moz-opacity: 0.5; /* Netscape */
-khtml-opacity: 0.5; /* Safari 1.x */
opacity: 0.5; /* Good browsers */
}
.full_opacity {
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=100)"; /* IE 8 */
filter: alpha(opacity=100); /* IE 5-7 */
-moz-opacity: 1.0; /* Netscape */
-khtml-opacity: 1.0; /* Safari 1.x */
opacity: 1.0; /* Good browsers */
}
Thursday, December 6, 2012
Installing Drupal Commons
Install xampp, or anything with Apache and MySQL
Create a database with any name for your Drupal Commons installation.
Get your download here:
https://network.acquia.com/downloads/drupal-commons
Extract these files, and move the resulting Drupal file directory structure into the web root, or any sub folder.
Configure a settings.php file from the example ( default.settings.php ) file provided.
Edit this line with your database information:
$db_url = 'mysql://username:password@localhost/databasename';
The simplest example ( only fun for a development site ) is:
$db_url = 'mysql://root@localhost/database_name';
Save settings.php
*** You will probably need to edit php.ini here to allow a maximum_execution longer than 30 seconds, this is a common fail. Try 300. Remember to restart Apache for the php.ini settings to take effect.
Use a web browser to execute the install.php file in the folder containing the installation.
Create a database with any name for your Drupal Commons installation.
Get your download here:
https://network.acquia.com/downloads/drupal-commons
Extract these files, and move the resulting Drupal file directory structure into the web root, or any sub folder.
Configure a settings.php file from the example ( default.settings.php ) file provided.
Edit this line with your database information:
$db_url = 'mysql://username:password@localhost/databasename';
The simplest example ( only fun for a development site ) is:
$db_url = 'mysql://root@localhost/database_name';
Save settings.php
*** You will probably need to edit php.ini here to allow a maximum_execution longer than 30 seconds, this is a common fail. Try 300. Remember to restart Apache for the php.ini settings to take effect.
Use a web browser to execute the install.php file in the folder containing the installation.
Wednesday, November 28, 2012
Detecting character encoding in PHP
Dealing with character encoding is never fun. It's easy to know that something has gone wrong when text is displaying as hieroglyphics, but it can be difficult to figure out why.
I found myself in this position trying to fix some Japanese text the other day and wanted to write a short example of mb_detect_encoding(); before I forget it.
<?php
// a list of possible encodings
$encodings = array('UTF-8','windows-1251','ISO-8859-1','ASCII','JIS','EUC-JP');
$string = "Example text"; // the text you wish to test
$in_encoding = mb_detect_encoding($string, $encodings, true);
echo "Encoding type: ".$in_encoding;
?>
If you find that your encoding is something other than what you desire, you should check out iconv();
I found myself in this position trying to fix some Japanese text the other day and wanted to write a short example of mb_detect_encoding(); before I forget it.
<?php
// a list of possible encodings
$encodings = array('UTF-8','windows-1251','ISO-8859-1','ASCII','JIS','EUC-JP');
$string = "Example text"; // the text you wish to test
$in_encoding = mb_detect_encoding($string, $encodings, true);
echo "Encoding type: ".$in_encoding;
?>
If you find that your encoding is something other than what you desire, you should check out iconv();
Subscribe to:
Posts (Atom)