workflow.views.inc 2.6 KB

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