Commit 6b9ebf7a authored by upchuk's avatar upchuk

Initial commit

parents
name = Editor List
description = Demo module illustrating a custom solution for having multiple editors on a node.
core = 7.x
dependencies[] = entityreference
\ No newline at end of file
<?php
/**
* Implements hook_install().
*/
function editor_list_install() {
// Creating the field_editors reference field on the Article node
$field_base = array(
'active' => 1,
'cardinality' => -1,
'deleted' => 0,
'entity_types' => array(),
'field_name' => 'field_editors',
'indexes' => array(
'target_id' => array(
0 => 'target_id',
),
),
'locked' => 0,
'module' => 'entityreference',
'settings' => array(
'entity_translation_sync' => FALSE,
'handler' => 'base',
'handler_settings' => array(
'behaviors' => array(
'views-select-list' => array(
'status' => 0,
),
),
'sort' => array(
'type' => 'none',
),
'target_bundles' => array(),
),
'target_type' => 'user',
),
'translatable' => 0,
'type' => 'entityreference',
);
field_create_field($field_base);
$field_instance = array(
'bundle' => 'article',
'default_value' => NULL,
'deleted' => 0,
'description' => '',
'display' => array(
'default' => array(
'label' => 'above',
'module' => 'entityreference',
'settings' => array(
'link' => FALSE,
),
'type' => 'entityreference_label',
'weight' => 3,
),
'teaser' => array(
'label' => 'above',
'settings' => array(),
'type' => 'hidden',
'weight' => 0,
),
),
'entity_type' => 'node',
'field_name' => 'field_editors',
'label' => 'Editors',
'required' => 0,
'settings' => array(
'entity_translation_sync' => FALSE,
'user_register_form' => FALSE,
),
'widget' => array(
'active' => 1,
'module' => 'entityreference',
'settings' => array(
'match_operator' => 'CONTAINS',
'path' => '',
'size' => 60,
),
'type' => 'entityreference_autocomplete',
'weight' => 6,
),
);
field_create_instance($field_instance);
}
\ No newline at end of file
<?php
/**
* Returns an array of node types we are targeting our editor lists for.
*
* @return array
*/
function editor_list_node_types() {
// If we need to add more node types, we just add them to this array.
return array('article');
}
/**
* Implements hook_node_access().
*/
function editor_list_node_access($node, $op, $account) {
$node_types = editor_list_node_types();
if ( ! is_object($node) || ! in_array($node->type, $node_types) || $op !== 'update') {
return NODE_ACCESS_IGNORE;
}
$allowed_uids = editor_list_uids_from_list($node);
if (empty($allowed_uids)) {
return NODE_ACCESS_IGNORE;
}
if (in_array($account->uid, $allowed_uids)) {
return NODE_ACCESS_ALLOW;
}
}
/**
* Implements hook_field_access().
*/
function editor_list_field_access($op, $field, $entity_type, $entity, $account) {
$node_types = editor_list_node_types();
if ($entity_type === 'node' && is_object($entity) && in_array($entity->type, $node_types)) {
return editor_list_control_field_access($op, $field, $entity_type, $entity, $account);
}
}
/**
* Controls field access for editor list targeted nodes.
*/
function editor_list_control_field_access($op, $field, $entity_type, $entity, $account) {
if ($op !== 'edit') {
return;
}
$uids = editor_list_uids_from_list($entity);
if (!in_array($account->uid, $uids)) {
return;
}
// Here we can add other field names to deny access if we want.
$deny = array('field_editors');
if (in_array($field['field_name'], $deny)) {
return false;
}
}
/**
* Implements hook_form_BASE_FORM_ID_alter().
*/
function editor_list_form_article_node_form_alter(&$form, &$form_state, $form_id) {
$form['#after_build'][] = 'editor_list_node_form_after_build';
}
/**
* After build function for node forms we use to move the editors list field
* into the authorship group
*/
function editor_list_node_form_after_build($form, &$form_state) {
// If the user doesn't have access to the editors list field, we do nothing.
$field = field_info_field('field_editors');
if ( ! field_access('edit', $field, 'node', $form['#entity'])) {
return $form;
}
// If the user doesn't have access to authorship group, we do nothing.
if ($form['author']['#access'] === 0) {
return $form;
}
$field_editors = $form['field_editors'];
$field_editors['#weight'] = 0;
$form['author']['additional_authors'] = $field_editors;
$form['field_editors'] = array();
return $form;
}
/**
* Helper function that returns the uids of the users in the editor list of a
* given node.
*
* @param $node
* The node object.
*
* @return array
* Array of user ids.
*/
function editor_list_uids_from_list($node) {
$users = field_get_items('node', $node, 'field_editors');
$allowed_uids = array();
if ($users) {
$allowed_uids = array_map(function($user) {
return $user['target_id'];
}, $users);
}
return $allowed_uids;
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment