workflow.views.inc 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /**
  3. * @file
  4. * Provide Views data for workflow.module.
  5. *
  6. * @ingroup views_module_handlers
  7. */
  8. use Drupal\Core\Entity\ContentEntityInterface;
  9. use Drupal\workflow\Entity\WorkflowManager;
  10. use Drupal\field\FieldStorageConfigInterface;
  11. /**
  12. * Implements hook_field_views_data().
  13. */
  14. function workflow_field_views_data(FieldStorageConfigInterface $field) {
  15. $data = views_field_default_views_data($field);
  16. $settings = $field->getSettings();
  17. foreach ($data as $table_name => $table_data) {
  18. foreach ($table_data as $field_name => $field_data) {
  19. if ($field_name == 'delta') {
  20. continue;
  21. }
  22. if (isset($field_data['filter'])) {
  23. $data[$table_name][$field_name]['filter']['wid'] = (array_key_exists('workflow_type', $settings)) ? $settings['workflow_type'] : '';
  24. $data[$table_name][$field_name]['filter']['id'] = 'workflow_state';
  25. }
  26. if (isset($field_data['argument'])) {
  27. $data[$table_name][$field_name]['argument']['id'] = 'workflow_state';
  28. }
  29. }
  30. }
  31. return $data;
  32. }
  33. /**
  34. * Implements hook_views_data_alter().
  35. */
  36. function workflow_views_data_alter(array &$data) {
  37. // Provide an integration for each entity type except workflow entities.
  38. // Copied from comment.views.inc.
  39. foreach (\Drupal::entityTypeManager()->getDefinitions() as $entity_type_id => $entity_type) {
  40. if (WorkflowManager::isWorkflowEntityType($entity_type_id)) {
  41. continue;
  42. }
  43. if (!$entity_type->entityClassImplements(ContentEntityInterface::class)) {
  44. continue;
  45. }
  46. if (!$entity_type->getBaseTable()) {
  47. continue;
  48. }
  49. $fields = \Drupal::service('workflow.manager')->getFields($entity_type_id);
  50. if ($fields) {
  51. $base_table = $entity_type->getDataTable() ?: $entity_type->getBaseTable();
  52. $args = ['@entity_type' => $entity_type_id];
  53. foreach ($fields as $field_name => $field) {
  54. $data[$base_table][$field_name . '_tid'] = [
  55. 'title' => t('Workflow transitions on @entity_type using field: @field_name', $args + ['@field_name' => $field_name]),
  56. 'help' => t('Relate all transitions ongit status @entity_type. This will create 1 duplicate record for every transition. Usually if you need this it is better to create a Transition view.', $args),
  57. 'relationship' => [
  58. 'group' => t('Workflow transition'),
  59. 'label' => t('workflow transition'),
  60. 'base' => 'workflow_transition_history',
  61. 'base field' => 'entity_id',
  62. 'relationship field' => $entity_type->getKey('id'),
  63. 'id' => 'standard',
  64. 'extra' => [
  65. [
  66. 'field' => 'entity_type',
  67. 'value' => $entity_type_id,
  68. ],
  69. [
  70. 'field' => 'field_name',
  71. 'value' => $field_name,
  72. ],
  73. ],
  74. ],
  75. ];
  76. }
  77. }
  78. }
  79. }