field.views.inc

  1. cis7 sites/all/modules/ulmus/views/modules/field.views.inc
  2. cle7 sites/all/modules/ulmus/views/modules/field.views.inc
  3. ecd7 sites/all/modules/ulmus/views/modules/field.views.inc
  4. elmsmedia7 sites/all/modules/ulmus/views/modules/field.views.inc
  5. harmony7 sites/all/modules/ulmus/views/modules/field.views.inc
  6. icor7 sites/all/modules/ulmus/views/modules/field.views.inc
  7. meedjum_blog7 sites/all/modules/ulmus/views/modules/field.views.inc
  8. mooc7 sites/all/modules/ulmus/views/modules/field.views.inc

Provide Views data and handlers for field.module.

Functions

Namesort descending Description
field_views_data Implements hook_views_data().
field_views_data_alter Implements hook_views_data_alter().
field_views_field_default_views_data Default views data implementation for a field.
field_views_field_label Returns the label of a certain field.
list_field_views_data Have a different filter handler for lists. This should allow to select values of the list.

File

sites/all/modules/ulmus/views/modules/field.views.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Provide Views data and handlers for field.module.
  5. *
  6. * @ingroup views_module_handlers
  7. */
  8. /**
  9. * Implements hook_views_data().
  10. *
  11. * Field modules can implement hook_field_views_data() to override
  12. * the default behavior for adding fields.
  13. */
  14. function field_views_data() {
  15. $data = array();
  16. foreach (field_info_fields() as $field) {
  17. if ($field['storage']['type'] != 'field_sql_storage') {
  18. continue;
  19. }
  20. $module = $field['module'];
  21. $result = (array) module_invoke($module, 'field_views_data', $field);
  22. if (empty($result)) {
  23. $result = field_views_field_default_views_data($field);
  24. }
  25. drupal_alter('field_views_data', $result, $field, $module);
  26. if (is_array($result)) {
  27. $data = drupal_array_merge_deep($result, $data);
  28. }
  29. }
  30. return $data;
  31. }
  32. /**
  33. * Implements hook_views_data_alter().
  34. *
  35. * Field modules can implement hook_field_views_data_views_data_alter() to
  36. * alter the views data on a per field basis. This is weirdly named so as
  37. * not to conflict with the drupal_alter('field_views_data') in
  38. * field_views_data.
  39. */
  40. function field_views_data_alter(&$data) {
  41. foreach (field_info_fields() as $field) {
  42. if ($field['storage']['type'] != 'field_sql_storage') {
  43. continue;
  44. }
  45. $function = $field['module'] . '_field_views_data_views_data_alter';
  46. if (function_exists($function)) {
  47. $function($data, $field);
  48. }
  49. }
  50. }
  51. /**
  52. * Returns the label of a certain field.
  53. *
  54. * Therefore it looks up in all bundles to find the most used instance.
  55. */
  56. function field_views_field_label($field_name) {
  57. $label_counter = array();
  58. $all_labels = array();
  59. // Count the amount of instances per label per field.
  60. $instances = field_info_instances();
  61. foreach ($instances as $entity_name => $entity_type) {
  62. foreach ($entity_type as $bundle) {
  63. if (isset($bundle[$field_name])) {
  64. $label_counter[$bundle[$field_name]['label']] = isset($label_counter[$bundle[$field_name]['label']]) ? ++$label_counter[$bundle[$field_name]['label']] : 1;
  65. $all_labels[$entity_name][$bundle[$field_name]['label']] = TRUE;
  66. }
  67. }
  68. }
  69. if (empty($label_counter)) {
  70. return array($field_name, $all_labels);
  71. }
  72. // Sort the field lables by it most used label and return the most used one.
  73. arsort($label_counter);
  74. $label_counter = array_keys($label_counter);
  75. return array($label_counter[0], $all_labels);
  76. }
  77. /**
  78. * Default views data implementation for a field.
  79. */
  80. function field_views_field_default_views_data($field) {
  81. $field_types = field_info_field_types();
  82. // Check the field module is available.
  83. if (!isset($field_types[$field['type']])) {
  84. return;
  85. }
  86. $data = array();
  87. $current_table = _field_sql_storage_tablename($field);
  88. $revision_table = _field_sql_storage_revision_tablename($field);
  89. // The list of entity:bundle that this field is used in.
  90. $bundles_names = array();
  91. $supports_revisions = FALSE;
  92. $entity_tables = array();
  93. $current_tables = array();
  94. $revision_tables = array();
  95. $groups = array();
  96. $group_name = count($field['bundles']) > 1 ? t('Field') : NULL;
  97. // Build the relationships between the field table and the entity tables.
  98. foreach ($field['bundles'] as $entity => $bundles) {
  99. $entity_info = entity_get_info($entity);
  100. $groups[$entity] = $entity_info['label'];
  101. // Override Node to Content.
  102. if ($groups[$entity] == t('Node')) {
  103. $groups[$entity] = t('Content');
  104. }
  105. // If only one bundle use this as the default name.
  106. if (empty($group_name)) {
  107. $group_name = $groups[$entity];
  108. }
  109. $entity_tables[$entity_info['base table']] = $entity;
  110. $current_tables[$entity] = $entity_info['base table'];
  111. if (isset($entity_info['revision table'])) {
  112. $entity_tables[$entity_info['revision table']] = $entity;
  113. $revision_tables[$entity] = $entity_info['revision table'];
  114. }
  115. $data[$current_table]['table']['join'][$entity_info['base table']] = array(
  116. 'left_field' => $entity_info['entity keys']['id'],
  117. 'field' => 'entity_id',
  118. 'extra' => array(
  119. array('field' => 'entity_type', 'value' => $entity),
  120. array('field' => 'deleted', 'value' => 0, 'numeric' => TRUE),
  121. ),
  122. );
  123. if (!empty($entity_info['entity keys']['revision']) && !empty($entity_info['revision table'])) {
  124. $data[$revision_table]['table']['join'][$entity_info['revision table']] = array(
  125. 'left_field' => $entity_info['entity keys']['revision'],
  126. 'field' => 'revision_id',
  127. 'extra' => array(
  128. array('field' => 'entity_type', 'value' => $entity),
  129. array('field' => 'deleted', 'value' => 0, 'numeric' => TRUE),
  130. ),
  131. );
  132. $supports_revisions = TRUE;
  133. }
  134. foreach ($bundles as $bundle) {
  135. $bundles_names[] = t('@entity:@bundle', array('@entity' => $entity, '@bundle' => $bundle));
  136. }
  137. }
  138. $tables = array();
  139. $tables[FIELD_LOAD_CURRENT] = $current_table;
  140. if ($supports_revisions) {
  141. $tables[FIELD_LOAD_REVISION] = $revision_table;
  142. }
  143. $add_fields = array('delta', 'language', 'bundle');
  144. foreach ($field['columns'] as $column_name => $attributes) {
  145. $add_fields[] = _field_sql_storage_columnname($field['field_name'], $column_name);
  146. }
  147. // Note: we don't have a label available here, because we are at the field
  148. // level, not at the instance level. So we just go through all instances
  149. // and take the one which is used the most frequently.
  150. $field_name = $field['field_name'];
  151. list($label, $all_labels) = field_views_field_label($field_name);
  152. foreach ($tables as $type => $table) {
  153. if ($type == FIELD_LOAD_CURRENT) {
  154. $group = $group_name;
  155. $old_column = 'entity_id';
  156. $column = $field['field_name'];
  157. }
  158. else {
  159. $group = t('@group (historical data)', array('@group' => $group_name));
  160. $old_column = 'revision_id';
  161. $column = $field['field_name'] . '-' . $old_column;
  162. }
  163. $data[$table][$old_column]['field']['moved to'] = array($table, $column);
  164. $data[$table][$column] = array(
  165. 'group' => $group,
  166. 'title' => $label,
  167. 'title short' => $label,
  168. 'help' => t('Appears in: @bundles.', array('@bundles' => implode(', ', $bundles_names))),
  169. );
  170. // Go through and create a list of aliases for all possible combinations of
  171. // entity type + name.
  172. $aliases = array();
  173. $also_known = array();
  174. foreach ($all_labels as $entity_name => $labels) {
  175. foreach ($labels as $label_name => $true) {
  176. if ($type == FIELD_LOAD_CURRENT) {
  177. if ($group_name != $groups[$entity_name] || $label != $label_name) {
  178. $aliases[] = array(
  179. 'base' => $current_tables[$entity_name],
  180. 'group' => $groups[$entity_name],
  181. 'title' => $label_name,
  182. 'help' => t('This is an alias of @group: @field.', array('@group' => $group_name, '@field' => $label)),
  183. );
  184. }
  185. $also_known[] = t('@group: @field', array('@group' => $groups[$entity_name], '@field' => $label_name));
  186. }
  187. else {
  188. if ($group_name != $groups[$entity_name] && $label != $label_name && isset($revision_tables[$entity_name])) {
  189. $aliases[] = array(
  190. 'base' => $revision_tables[$entity_name],
  191. 'group' => t('@group (historical data)', array('@group' => $groups[$entity_name])),
  192. 'title' => $label_name,
  193. 'help' => t('This is an alias of @group: @field.', array('@group' => $group_name, '@field' => $label)),
  194. );
  195. }
  196. $also_known[] = t('@group (historical data): @field', array('@group' => $groups[$entity_name], '@field' => $label_name));
  197. }
  198. }
  199. }
  200. if ($aliases) {
  201. $data[$table][$column]['aliases'] = $aliases;
  202. $data[$table][$column]['help'] .= ' ' . t('Also known as: !also.', array('!also' => implode(', ', $also_known)));
  203. }
  204. $keys = array_keys($field['columns']);
  205. $real_field = reset($keys);
  206. $data[$table][$column]['field'] = array(
  207. 'table' => $table,
  208. 'handler' => 'views_handler_field_field',
  209. 'click sortable' => TRUE,
  210. 'field_name' => $field['field_name'],
  211. // Provide a real field for group by.
  212. 'real field' => $column . '_' . $real_field,
  213. 'additional fields' => $add_fields,
  214. 'entity_tables' => $entity_tables,
  215. // Default the element type to div, let the UI change it if necessary.
  216. 'element type' => 'div',
  217. 'is revision' => $type == FIELD_LOAD_REVISION,
  218. );
  219. }
  220. foreach ($field['columns'] as $column => $attributes) {
  221. $allow_sort = TRUE;
  222. // Identify likely filters and arguments for each column based on field type.
  223. switch ($attributes['type']) {
  224. case 'int':
  225. case 'mediumint':
  226. case 'tinyint':
  227. case 'bigint':
  228. case 'serial':
  229. case 'numeric':
  230. case 'float':
  231. $filter = 'views_handler_filter_numeric';
  232. $argument = 'views_handler_argument_numeric';
  233. $sort = 'views_handler_sort';
  234. break;
  235. case 'text':
  236. case 'blob':
  237. // It does not make sense to sort by blob or text.
  238. $allow_sort = FALSE;
  239. default:
  240. $filter = 'views_handler_filter_string';
  241. $argument = 'views_handler_argument_string';
  242. $sort = 'views_handler_sort';
  243. break;
  244. }
  245. if (count($field['columns']) == 1 || $column == 'value') {
  246. $title = t('@label (!name)', array('@label' => $label, '!name' => $field['field_name']));
  247. // CCK used the first 10 characters of $label. Probably doesn't matter.
  248. $title_short = $label;
  249. }
  250. else {
  251. $title = t('@label (!name:!column)', array('@label' => $label, '!name' => $field['field_name'], '!column' => $column));
  252. $title_short = t('@label:!column', array('@label' => $label, '!column' => $column));
  253. }
  254. foreach ($tables as $type => $table) {
  255. if ($type == FIELD_LOAD_CURRENT) {
  256. $group = $group_name;
  257. }
  258. else {
  259. $group = t('@group (historical data)', array('@group' => $group_name));
  260. }
  261. $column_real_name = $field['storage']['details']['sql'][$type][$table][$column];
  262. // Load all the fields from the table by default.
  263. $additional_fields = array_values($field['storage']['details']['sql'][$type][$table]);
  264. $data[$table][$column_real_name] = array(
  265. 'group' => $group,
  266. 'title' => $title,
  267. 'title short' => $title_short,
  268. 'help' => t('Appears in: @bundles.', array('@bundles' => implode(', ', $bundles_names))),
  269. );
  270. // Go through and create a list of aliases for all possible combinations of
  271. // entity type + name.
  272. $aliases = array();
  273. $also_known = array();
  274. foreach ($all_labels as $entity_name => $labels) {
  275. foreach ($labels as $label_name => $true) {
  276. if ($group_name != $groups[$entity_name] || $label != $label_name) {
  277. if (count($field['columns']) == 1 || $column == 'value') {
  278. $alias_title = t('@label (!name)', array('@label' => $label_name, '!name' => $field['field_name']));
  279. // CCK used the first 10 characters of $label. Probably doesn't matter.
  280. }
  281. else {
  282. $alias_title = t('@label (!name:!column)', array('@label' => $label_name, '!name' => $field['field_name'], '!column' => $column));
  283. }
  284. $aliases[] = array(
  285. 'group' => $groups[$entity_name],
  286. 'title' => $alias_title,
  287. 'help' => t('This is an alias of @group: @field.', array('@group' => $group_name, '@field' => $title)),
  288. );
  289. }
  290. $also_known[] = t('@group: @field', array('@group' => $groups[$entity_name], '@field' => $title));
  291. }
  292. }
  293. if ($aliases) {
  294. $data[$table][$column_real_name]['aliases'] = $aliases;
  295. $data[$table][$column_real_name]['help'] .= ' ' . t('Also known as: !also.', array('!also' => implode(', ', $also_known)));
  296. }
  297. $data[$table][$column_real_name]['argument'] = array(
  298. 'field' => $column_real_name,
  299. 'table' => $table,
  300. 'handler' => $argument,
  301. 'additional fields' => $additional_fields,
  302. 'field_name' => $field['field_name'],
  303. 'empty field name' => t('- No value -'),
  304. );
  305. $data[$table][$column_real_name]['filter'] = array(
  306. 'field' => $column_real_name,
  307. 'table' => $table,
  308. 'handler' => $filter,
  309. 'additional fields' => $additional_fields,
  310. 'field_name' => $field['field_name'],
  311. 'allow empty' => TRUE,
  312. );
  313. if (!empty($allow_sort)) {
  314. $data[$table][$column_real_name]['sort'] = array(
  315. 'field' => $column_real_name,
  316. 'table' => $table,
  317. 'handler' => $sort,
  318. 'additional fields' => $additional_fields,
  319. 'field_name' => $field['field_name'],
  320. );
  321. }
  322. // Expose additional delta column for multiple value fields.
  323. if ($field['cardinality'] > 1 || $field['cardinality'] == FIELD_CARDINALITY_UNLIMITED) {
  324. $title_delta = t('@label (!name:delta)', array('@label' => $label, '!name' => $field['field_name']));
  325. $title_short_delta = t('@label:delta', array('@label' => $label));
  326. $data[$table]['delta'] = array(
  327. 'group' => $group,
  328. 'title' => $title_delta,
  329. 'title short' => $title_short_delta,
  330. 'help' => t('Delta - Appears in: @bundles.', array('@bundles' => implode(', ', $bundles_names))),
  331. );
  332. $data[$table]['delta']['field'] = array(
  333. 'handler' => 'views_handler_field_numeric',
  334. );
  335. $data[$table]['delta']['argument'] = array(
  336. 'field' => 'delta',
  337. 'table' => $table,
  338. 'handler' => 'views_handler_argument_numeric',
  339. 'additional fields' => $additional_fields,
  340. 'empty field name' => t('- No value -'),
  341. 'field_name' => $field['field_name'],
  342. );
  343. $data[$table]['delta']['filter'] = array(
  344. 'field' => 'delta',
  345. 'table' => $table,
  346. 'handler' => 'views_handler_filter_numeric',
  347. 'additional fields' => $additional_fields,
  348. 'field_name' => $field['field_name'],
  349. 'allow empty' => TRUE,
  350. );
  351. $data[$table]['delta']['sort'] = array(
  352. 'field' => 'delta',
  353. 'table' => $table,
  354. 'handler' => 'views_handler_sort',
  355. 'additional fields' => $additional_fields,
  356. 'field_name' => $field['field_name'],
  357. );
  358. }
  359. // Expose additional language column for translatable fields.
  360. if (!empty($field['translatable'])) {
  361. $title_language = t('@label (!name:language)', array('@label' => $label, '!name' => $field['field_name']));
  362. $title_short_language = t('@label:language', array('@label' => $label));
  363. $data[$table]['language'] = array(
  364. 'group' => $group,
  365. 'title' => $title_language,
  366. 'title short' => $title_short_language,
  367. 'help' => t('Language - Appears in: @bundles.', array('@bundles' => implode(', ', $bundles_names))),
  368. );
  369. $data[$table]['language']['field'] = array(
  370. 'handler' => 'views_handler_field_locale_language',
  371. );
  372. $data[$table]['language']['argument'] = array(
  373. 'field' => 'language',
  374. 'table' => $table,
  375. 'handler' => 'views_handler_argument_locale_language',
  376. 'additional fields' => $additional_fields,
  377. 'empty field name' => t('<No value>'),
  378. 'field_name' => $field['field_name'],
  379. );
  380. $data[$table]['language']['filter'] = array(
  381. 'field' => 'language',
  382. 'table' => $table,
  383. 'handler' => 'views_handler_filter_locale_language',
  384. 'additional fields' => $additional_fields,
  385. 'field_name' => $field['field_name'],
  386. 'allow empty' => TRUE,
  387. );
  388. $data[$table]['language']['sort'] = array(
  389. 'field' => 'language',
  390. 'table' => $table,
  391. 'handler' => 'views_handler_sort',
  392. 'additional fields' => $additional_fields,
  393. 'field_name' => $field['field_name'],
  394. );
  395. }
  396. // Expose additional language column for translatable fields.
  397. if (!empty($field['translatable'])) {
  398. $title_language = t('@label (!name:language)', array('@label' => $label, '!name' => $field['field_name']));
  399. $title_short_language = t('@label:language', array('@label' => $label));
  400. $data[$table]['language'] = array(
  401. 'group' => $group,
  402. 'title' => $title_language,
  403. 'title short' => $title_short_language,
  404. 'help' => t('Language - Appears in: @bundles.', array('@bundles' => implode(', ', $bundles_names))),
  405. );
  406. $data[$table]['language']['field'] = array(
  407. 'handler' => 'views_handler_field_locale_language',
  408. );
  409. $data[$table]['language']['argument'] = array(
  410. 'field' => 'language',
  411. 'table' => $table,
  412. 'handler' => 'views_handler_argument_locale_language',
  413. 'additional fields' => $additional_fields,
  414. 'empty field name' => t('<No value>'),
  415. 'field_name' => $field['field_name'],
  416. );
  417. $data[$table]['language']['filter'] = array(
  418. 'field' => 'language',
  419. 'table' => $table,
  420. 'handler' => 'views_handler_filter_locale_language',
  421. 'additional fields' => $additional_fields,
  422. 'field_name' => $field['field_name'],
  423. 'allow empty' => TRUE,
  424. );
  425. $data[$table]['language']['sort'] = array(
  426. 'field' => 'language',
  427. 'table' => $table,
  428. 'handler' => 'views_handler_sort',
  429. 'additional fields' => $additional_fields,
  430. 'field_name' => $field['field_name'],
  431. );
  432. }
  433. }
  434. }
  435. return $data;
  436. }
  437. /**
  438. * Have a different filter handler for lists. This should allow to select values of the list.
  439. */
  440. function list_field_views_data($field) {
  441. $data = field_views_field_default_views_data($field);
  442. foreach ($data as $table_name => $table_data) {
  443. foreach ($table_data as $field_name => $field_data) {
  444. if (isset($field_data['filter']) && $field_name != 'delta') {
  445. $data[$table_name][$field_name]['filter']['handler'] = 'views_handler_filter_field_list';
  446. }
  447. if (isset($field_data['argument']) && $field_name != 'delta') {
  448. if ($field['type'] == 'list_text') {
  449. $data[$table_name][$field_name]['argument']['handler'] = 'views_handler_argument_field_list_string';
  450. }
  451. else {
  452. $data[$table_name][$field_name]['argument']['handler'] = 'views_handler_argument_field_list';
  453. }
  454. }
  455. }
  456. }
  457. return $data;
  458. }

Related topics