| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322 |
- <?php
- /**
- * Implements hook_field_info().
- */
- function rolereference_field_info() {
- return array(
- 'rolereference' => array(
- 'label' => t('Role reference'),
- 'description' => t('This field stores the ID of a related role as an integer value.'),
- 'settings' => array('referenceable_roles' => array()),
- 'default_widget' => 'options_select',
- 'default_formatter' => 'rolereference_default',
- 'property_type' => 'integer',
- ),
- );
- }
- /**
- * Implements hook_field_schema().
- */
- function rolereference_field_schema($field) {
- $columns = array(
- 'rid' => array(
- 'type' => 'int',
- 'unsigned' => TRUE,
- 'not null' => FALSE,
- ),
- );
- return array(
- 'columns' => $columns,
- 'indexes' => array('rid' => array('rid')),
- 'foreign keys' => array(
- 'rid' => array(
- 'table' => 'role',
- 'columns' => array('rid' => 'rid'),
- ),
- ),
- );
- }
- /**
- * Implements hook_field_settings_form().
- */
- function rolereference_field_settings_form($field, $instance, $has_data) {
- $settings = $field['settings'];
- $form = array();
- $role_options = array_map('check_plain', user_roles());
- $form['referenceable_roles'] = array(
- '#type' => 'checkboxes',
- '#title' => t('Roles that can be referenced'),
- '#description' => t('Select roles that may be referenced by this field. All roles will be available if none are checked.'),
- '#multiple' => TRUE,
- '#default_value' => is_array($settings['referenceable_roles']) ? $settings['referenceable_roles'] : array(),
- '#options' => $role_options,
- '#disabled' => $has_data,
- );
- return $form;
- }
- /**
- * Implements hook_field_validate().
- *
- * Possible error codes:
- * - 'invalid_rid': nid is not valid for the field
- * (not a valid role id, or the role is not referenceable).
- */
- function rolereference_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
- // Extract nids to check.
- $ids = array();
- // First check non-numeric rid's to avoid losing time with them.
- foreach ($items as $delta => $item) {
- if (is_array($item) && !empty($item['rid'])) {
- if (is_numeric($item['rid'])) {
- $ids[] = $item['rid'];
- }
- else {
- $errors[$field['field_name']][$langcode][$delta][] = array(
- 'error' => 'invalid_rid',
- 'message' => t("%name: invalid input.",
- array('%name' => $instance['label'])),
- );
- }
- }
- }
- // Prevent performance hog if there are no ids to check.
- if ($ids) {
- $referenceable_roles = rolereference_potential_references($field['settings']['referenceable_roles'], $field['field_name']);
- foreach ($items as $delta => $item) {
- if (is_array($item)) {
- if (!empty($item['rid']) && !isset($referenceable_roles[$item['rid']])) {
- $errors[$field['field_name']][$langcode][$delta][] = array(
- 'error' => 'invalid_rid',
- 'message' => t("%name: this role can't be referenced.",
- array('%name' => $instance['label'])),
- );
- }
- }
- }
- }
- }
- /**
- * Implements hook_field_prepare_view().
- */
- function rolereference_field_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items) {
- $referenceable_roles = rolereference_potential_references($field['settings']['referenceable_roles'], $field['field_name']);
- // Extract nids to check.
- $ids = array();
- foreach ($items as $id => $entity_items) {
- foreach ($entity_items as $delta => $item) {
- if (is_array($item)) {
- // Default to 'not accessible'.
- $items[$id][$delta]['access'] = FALSE;
- if (!empty($item['rid']) && is_numeric($item['rid'])) {
- $ids[$item['rid']] = $item['rid'];
- }
- }
- }
- }
- if ($ids) {
- foreach ($items as $id => $entity_items) {
- foreach ($entity_items as $delta => $item) {
- if (is_array($item) && !empty($item['rid']) && isset($referenceable_roles[$item['rid']])) {
- $items[$id][$delta]['role'] = $referenceable_roles[$item['rid']];
- $items[$id][$delta]['access'] = TRUE;
- }
- }
- }
- }
- }
- /**
- * Implements hook_field_is_empty().
- */
- function rolereference_field_is_empty($item, $field) {
- if (empty($item['rid']) && (string) $item['rid'] !== '0') {
- return TRUE;
- }
- return FALSE;
- }
- /**
- * Implements hook_field_formatter_info().
- */
- function rolereference_field_formatter_info() {
- $ret = array(
- 'rolereference_default' => array(
- 'label' => t('Title'),
- 'description' => t('Display the title of the referenced role.'),
- 'field types' => array('rolereference'),
- ),
- );
- return $ret;
- }
- /**
- * Implements hook_field_formatter_view().
- */
- function rolereference_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
- $element = array();
- switch ($display['type']) {
- case 'rolereference_default':
- foreach ($items as $delta => $item) {
- $element[$delta] = array('#markup' => check_plain($item['role']));
- }
- break;
- }
- return $element;
- }
- /**
- * Implements hook_field_widget_info_alter().
- */
- function rolereference_field_widget_info_alter(&$info) {
- $info['options_select']['field types'][] = 'rolereference';
- $info['options_buttons']['field types'][] = 'rolereference';
- }
- /**
- * Implements hook_options_list().
- */
- function rolereference_options_list($field) {
- return rolereference_potential_references($field['settings']['referenceable_roles'], $field['field_name']);
- }
- /**
- * Implements hook_views_api().
- */
- function rolereference_views_api() {
- return array(
- 'api' => '3.0',
- 'path' => drupal_get_path('module', 'rolereference') . '/includes',
- );
- }
- /**
- * Implements hook_field_views_data().
- *
- * In addition to the default field information we add the relationship for
- * views to connect back to the role table.
- */
- function rolereference_field_views_data($field) {
- $data = field_views_field_default_views_data($field);
- $storage = $field['storage']['details']['sql'];
- foreach ($storage as $age => $table_data) {
- $table = key($table_data);
- $columns = current($table_data);
- $id_column = $columns['rid'];
- if (isset($data[$table])) {
- $data[$table][$id_column]['relationship'] = array(
- 'base' => 'role',
- 'field' => $columns['rid'],
- 'base field' => 'rid',
- 'label' => $field['field_name'],
- );
- }
- }
- return $data;
- }
- /**
- * Return a rid->name array of referenceable roles, based on the field settings.
- *
- * @param {array} $referenceable_roles
- */
- function rolereference_potential_references($referenceable_roles, $field_name) {
- $roles = &drupal_static('rolereference_' . $field_name, array());
- $user_roles = user_roles();
- if (empty($roles)) {
- foreach ($user_roles as $rid => $role) {
- if (!empty($referenceable_roles[$rid])) {
- $roles[$rid] = $role;
- }
- }
- if (empty($roles)) {
- $roles = $user_roles;
- }
- }
- return $roles;
- }
- /**
- * Implements hook_content_migrate_field_alter().
- */
- function rolereference_content_migrate_field_alter(&$field_value, $instance_value) {
- switch ($instance_value['widget']['module']) {
- case 'rolereference':
- $field_value['settings']['referenceable_roles'] = $field_value['settings']['referenceable_types'];
- unset($field_value['settings']['referenceable_types']);
- break;
- }
- }
- /**
- * Implements hook_field_extract_info().
- *
- * This provides integration for the field_extract module,
- * making it easy to get the value of a rolereference field.
- *
- * Example: field_extract_value('node', $node, 'field_subscriber_role');
- *
- * @return $extractors
- * array: field_extract info for rolereference module.
- */
- function rolereference_field_extract_info() {
- $extractors = array(
- 'rolereference' => array(
- 'options' => array(
- 'key' => 'rid',
- ),
- ),
- );
- return $extractors;
- }
- /**
- * Implements feeds_processor_targets_alter().
- */
- function rolereference_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_name) {
- // Set up feeds mapper for rolereference fields.
- foreach (field_info_instances($entity_type, $bundle_name) as $name => $instance) {
- $info = field_info_field($name);
- if ($info['type'] == 'rolereference') {
- $targets[$name] = array(
- 'name' => check_plain($instance['label']),
- 'description' => t('Role reference field.'),
- 'callback' => 'rolereference_set_target',
- );
- }
- }
- }
- /**
- * Feeds callback to set mapping target.
- */
- function rolereference_set_target($source, $entity, $target, array $values, $mapping) {
- // Process rolereference field data.
- $info = field_info_field($target);
- if ($info['type'] == 'rolereference') {
- foreach ($values as $rid) {
- if (!empty($rid)) {
- $entity->{$target}[$entity->language][] = array('rid' => $rid);
- }
- }
- }
- }
|