Skip to content

Commit

Permalink
Issue backdrop#1077: Add a condition for selecting a specific node ID
Browse files Browse the repository at this point in the history
  • Loading branch information
docwilmot committed Feb 7, 2016
1 parent 4acf149 commit a9276a9
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
8 changes: 8 additions & 0 deletions core/modules/layout/includes/layout.layout.inc
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ function layout_layout_access_info() {
'description' => t('Control access by the currently active interface language.'),
'class' => 'LanguageLayoutAccess',
);
$info['node_id'] = array(
'title' => t('Node: ID'),
'description' => t('Control access by the current node ID.'),
'class' => 'NodeIDAccess',
'required contexts' => array(
'node' => 'node',
),
);
$info['user_permission'] = array(
'title' => t('User: Permission'),
'description' => t('Control access by permission string.'),
Expand Down
1 change: 1 addition & 0 deletions core/modules/layout/layout.module
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,7 @@ function layout_autoload_info() {
'LayoutAccessNegatable' => 'plugins/access/layout_access.inc',
'LanguageLayoutAccess' => 'plugins/access/language_layout_access.inc',
'FrontLayoutAccess' => 'plugins/access/front_layout_access.inc',
'NodeIDAccess' => 'plugins/access/node_id_access.inc',
'PathLayoutAccess' => 'plugins/access/path_layout_access.inc',
'UserRoleLayoutAccess' => 'plugins/access/user_role_layout_access.inc',
'UserPermissionLayoutAccess' => 'plugins/access/user_permission_layout_access.inc',
Expand Down
62 changes: 62 additions & 0 deletions core/modules/layout/plugins/access/node_id_access.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/**
* @file
* Plugin to provide access control based upon node ID.
*/
class NodeIDAccess extends LayoutAccessNegatable {
/**
* Constructor for a Layout access rule.
*/
function __construct($plugin_name, array $data = array()) {
parent::__construct($plugin_name, $data);
$this->settings += array(
'node_id' => NULL,
);
}

/**
* {@inheritdoc}
*/
function form(&$form, &$form_state) {
parent::form($form, $form_state);
$form['node_id'] = array(
'#type' => 'textfield',
'#title' => t('Node ID is'),
'#weight' => 100,
'#default_value' => (int) $this->settings['node_id'],
);
}

/**
* {@inheritdoc}
*/
function formSubmit($form, &$form_state) {
parent::formSubmit($form, $form_state);
$this->settings['node_id'] = $form_state['values']['node_id'];
}

/**
* {@inheritdoc}
*/
function checkAccess() {
$node = $this->contexts['node']->data;
if ($this->settings['negate']) {
return ($node->nid != $this->settings['node_id']);
}
else {
return ($node->nid == $this->settings['node_id']);
}
}

/**
* {@inheritdoc}
*/
function summary() {
if ($this->settings['negate']) {
return t('Node ID is NOT: @node_id', array('@node_id' => $this->settings['node_id']));
}
else {
return t('Node ID is: @node_id', array('@node_id' => $this->settings['node_id']));
}
}
}

0 comments on commit a9276a9

Please sign in to comment.