Projet

Général

Profil

Paste
Télécharger (5,15 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / entityreference / views / entityreference.views.inc @ 76df55b7

1
<?php
2

    
3
/**
4
 * @file
5
 * Views integration for Entity Reference.
6
 */
7

    
8
/**
9
 * Implements hook_field_views_data().
10
 */
11
function entityreference_field_views_data($field) {
12
  $data = field_views_field_default_views_data($field);
13
  $entity_info = entity_get_info($field['settings']['target_type']);
14
  foreach ($data as $table_name => $table_data) {
15
    if (isset($entity_info['base table'])) {
16
      $entity = $entity_info['label'];
17
      if ($entity == t('Node')) {
18
        $entity = t('Content');
19
      }
20

    
21
      $field_name = $field['field_name'] . '_target_id';
22
      $parameters = array('@entity' => $entity, '!field_name' => $field['field_name']);
23
      $data[$table_name][$field_name]['relationship'] = array(
24
        'handler' => 'views_handler_relationship',
25
        'base' => $entity_info['base table'],
26
        'base field' => $entity_info['entity keys']['id'],
27
        'label' => t('@entity entity referenced from !field_name', $parameters),
28
        'group' => t('Entity Reference'),
29
        'title' => t('Referenced Entity'),
30
        'help' => t('A bridge to the @entity entity that is referenced via !field_name', $parameters),
31
      );
32
    }
33
  }
34

    
35
  // Invoke the behaviors to allow them to change the properties.
36
  foreach (entityreference_get_behavior_handlers($field) as $handler) {
37
    $handler->views_data_alter($data, $field);
38
  }
39

    
40
  return $data;
41
}
42

    
43
/**
44
 * Options callback for Views handler views_handler_filter_in_operator.
45
 */
46
function entityreference_views_handler_options_list($field_name) {
47
  $field = field_info_field($field_name);
48
  return entityreference_options_list($field);
49
}
50

    
51
/**
52
 * Implements hook_field_views_data_views_data_alter().
53
 *
54
 * Views integration to provide reverse relationships on entityreference fields.
55
 */
56
function entityreference_field_views_data_views_data_alter(&$data, $field) {
57
  foreach ($field['bundles'] as $entity_type => $bundles) {
58
    $target_entity_info = entity_get_info($field['settings']['target_type']);
59
    if (isset($target_entity_info['base table'])) {
60
      $entity_info = entity_get_info($entity_type);
61
      $entity = $entity_info['label'];
62
      if ($entity == t('Node')) {
63
        $entity = t('Content');
64
      }
65
      $target_entity = $target_entity_info['label'];
66
      if ($target_entity == t('Node')) {
67
        $target_entity = t('Content');
68
      }
69

    
70
      $pseudo_field_name = 'reverse_' . $field['field_name'] . '_' . $entity_type;
71
      $replacements = array('@entity' => $entity, '@target_entity' => $target_entity, '!field_name' => $field['field_name']);
72
      $data[$target_entity_info['base table']][$pseudo_field_name]['relationship'] = array(
73
        'handler' => 'views_handler_relationship_entity_reverse',
74
        'field_name' => $field['field_name'],
75
        'field table' => _field_sql_storage_tablename($field),
76
        'field field' => $field['field_name'] . '_target_id',
77
        'base' => $entity_info['base table'],
78
        'base field' => $entity_info['entity keys']['id'],
79
        'label' => t('@entity referencing @target_entity from !field_name', $replacements),
80
        'group' => t('Entity Reference'),
81
        'title' => t('Referencing entity'),
82
        'help' => t('A bridge to the @entity entity that is referencing @target_entity via !field_name', $replacements),
83
        'join_extra' => array(
84
          0 => array(
85
            'field' => 'entity_type',
86
            'value' => $entity_type,
87
          ),
88
          1 => array(
89
            'field' => 'deleted',
90
            'value' => 0,
91
            'numeric' => TRUE,
92
          ),
93
        ),
94
      );
95
    }
96
  }
97
}
98

    
99
/**
100
 * Implements hook_views_plugins().
101
 */
102
function entityreference_views_plugins() {
103
  $plugins = array(
104
    'display' => array(
105
      'entityreference' => array(
106
        'title' => t('Entity Reference'),
107
        'admin' => t('Entity Reference Source'),
108
        'help' => 'Selects referenceable entities for an entity reference field',
109
        'handler' => 'entityreference_plugin_display',
110
        'uses hook menu' => FALSE,
111
        'use ajax' => FALSE,
112
        'use pager' => FALSE,
113
        'accept attachments' => FALSE,
114
        // Custom property, used with views_get_applicable_views() to retrieve
115
        // all views with a 'Entity Reference' display.
116
        'entityreference display' => TRUE,
117
      ),
118
    ),
119
    'style' => array(
120
      'entityreference_style' => array(
121
        'title' => t('Entity Reference list'),
122
        'help' => 'Returns results as a PHP array of labels and rendered rows.',
123
        'handler' => 'entityreference_plugin_style',
124
        'theme' => 'views_view_unformatted',
125
        'uses row plugin' => TRUE,
126
        'uses fields' => TRUE,
127
        'uses options' => TRUE,
128
        'type' => 'entityreference',
129
        'even empty' => TRUE,
130
      ),
131
    ),
132
    'row' => array(
133
      'entityreference_fields' => array(
134
        'title' => t('Inline fields'),
135
        'help' => t('Displays the fields with an optional template.'),
136
        'handler' => 'entityreference_plugin_row_fields',
137
        'theme' => 'views_view_fields',
138
        'theme path' => drupal_get_path('module', 'views') . '/theme',
139
        'theme file' => 'theme.inc',
140
        'uses fields' => TRUE,
141
        'uses options' => TRUE,
142
        'type' => 'entityreference',
143
      ),
144
    ),
145
  );
146
  return $plugins;
147
}