workflowfield.field.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <?php
  2. /**
  3. * @file
  4. * Defines a Workflow field, widget and formatter. (copied from list field).
  5. */
  6. /**
  7. * Implements hook_field_info().
  8. */
  9. function workflowfield_field_info() {
  10. return WorkflowItem::getInfo();
  11. }
  12. /**
  13. * Implements hook_form_FORM_ID_alter().
  14. *
  15. * Changes the hook_field_settings_form.
  16. * Fixes some Field settings and Field Instance settings, and makes sure users cannot change it.
  17. *
  18. * @todo: perhaps this is core functionality, but these values are only saved
  19. * when the site builder explicitly save the instance settings. :-(
  20. */
  21. function workflowfield_form_field_ui_field_edit_form_alter(&$form, &$form_state, $form_id) {
  22. if ($form['#field']['type'] == 'workflow') {
  23. // The Workflow field must have a value, so set to required.
  24. $form['instance']['required']['#default_value'] = 1;
  25. $form['instance']['required']['#disabled'] = TRUE;
  26. // User may not set a default value (this is done by the Workflow module).
  27. // @see WorkflowState::getOptions()
  28. $form['instance']['default_value_widget']['#type'] = 'hidden';
  29. $form['instance']['default_value_widget']['#disabled'] = TRUE;
  30. unset($form['instance']['default_value_widget']);
  31. // Make sure only 1 value can be entered in the Workflow field.
  32. $form['field']['cardinality']['#default_value'] = 1;
  33. $form['field']['cardinality']['#disabled'] = TRUE;
  34. }
  35. }
  36. /**
  37. * Implements hook_field_settings_form().
  38. */
  39. function workflowfield_field_settings_form($field, $instance, $has_data) {
  40. $form = array();
  41. $form_state = array();
  42. $workflow_field = new WorkflowItem($field, $instance);
  43. return $workflow_field->settingsForm($form, $form_state, $has_data);
  44. }
  45. /**
  46. * Implements property_callbacks for hook_field_info().
  47. */
  48. function workflowfield_property_info_callback(&$info, $entity_type, $field, $instance, $field_type) {
  49. $property = &$info[$entity_type]['bundles'][$instance['bundle']]['properties'][$field['field_name']];
  50. $property['getter callback'] = 'entity_metadata_field_property_get';
  51. $property['getter callback'] = '_workflowfield_metadata_property_get';
  52. // $property['setter callback'] = 'entity_metadata_field_property_set';
  53. // $property['setter callback'] = '_workflowfield_metadata_property_set';
  54. $property['options list'] = 'entity_metadata_field_options_list';
  55. $property['property info'] = array(
  56. 'value' => array(
  57. 'type' => 'integer',
  58. 'label' => t('State ID'),
  59. 'setter callback' => 'entity_property_verbatim_set',
  60. ),
  61. 'workflow' => array(
  62. 'type' => 'array',
  63. 'label' => t('Workflow details'),
  64. 'setter callback' => 'entity_property_verbatim_set',
  65. ),
  66. );
  67. }
  68. /**
  69. * Getter callback for Workflow defined in hook_entity_property_info_alter.
  70. *
  71. * This is different from the default, because 'value' is not always set
  72. * and 'workflow' may be set, but is not in the field data.
  73. */
  74. function _workflowfield_metadata_property_get($entity, array $options, $name, $entity_type, $info) {
  75. $values = array();
  76. // return entity_metadata_field_property_get($entity, array $options, $name, $entity_type, $info);
  77. $field = field_info_field($name);
  78. $columns = array_keys($field['columns']);
  79. $langcode = isset($options['language']) ? $options['language']->language : LANGUAGE_NONE;
  80. $langcode = entity_metadata_field_get_language($entity_type, $entity, $field, $langcode, TRUE);
  81. if (isset($entity->{$name}[$langcode])) {
  82. foreach ($entity->{$name}[$langcode] as $delta => $data) {
  83. // In workflowfield_property_info_callback(), we needed to set a column 'value'.
  84. // This is now filled from the widget data.
  85. // Sometimes that is not widget, or the submit function has not been processed yet.
  86. // On a normal widget:
  87. $sid = isset($data['value']) ? $data['value'] : 0;
  88. // On a workflow form widget:
  89. $sid = isset($data['workflow']['workflow_sid']) ? $data['workflow']['workflow_sid'] : $sid;
  90. // The workflow widget was not loaded properly. @see #2597307.
  91. // So we need to reload the entity to extract the correct value.
  92. if (!$sid && isset($data['workflow'])) {
  93. // $new_entity = $entity->original; // is NULL :-(
  94. list($entity_id, , ) = entity_extract_ids($entity_type, $entity);
  95. if (!empty($entity_id)) {
  96. $new_entity = entity_load_single($entity_type, $entity_id);
  97. $workflow = $new_entity->{$name}[$langcode][$delta];
  98. $sid = isset($workflow['value']) ? (int)$workflow['value'] : 0;
  99. }
  100. }
  101. $data[$columns[0]] = $sid;
  102. $values[$delta] = $sid;
  103. }
  104. }
  105. // For an empty single-valued field, we have to return NULL.
  106. return $field['cardinality'] == 1 ? ($values ? reset($values) : NULL) : $values;
  107. }
  108. /**
  109. * Callback for setting field property values.
  110. */
  111. function _workflowfield_metadata_property_set($entity, $name, $value, $langcode, $entity_type, $info) {
  112. // return entity_metadata_field_property_set($entity, $name, $value, $langcode, $entity_type, $info);
  113. $field = field_info_field($name);
  114. $columns = array_keys($field['columns']);
  115. $langcode = entity_metadata_field_get_language($entity_type, $entity, $field, $langcode);
  116. $values = $field['cardinality'] == 1 ? array($value) : (array) $value;
  117. }
  118. /**
  119. * We will be using some default formatters and widgets from the List and Options modules.
  120. */
  121. /**
  122. * Implements hook_field_formatter_info_alter().
  123. *
  124. * The module reuses the formatters defined in list.module.
  125. */
  126. function workflowfield_field_formatter_info_alter(&$info) {
  127. $info['list_default']['field types'][] = 'workflow';
  128. }
  129. /**
  130. * Implements hook_field_widget_info_alter().
  131. *
  132. * The module does not implement widgets of its own, but reuses the
  133. * widgets defined in options.module.
  134. *
  135. * @see workflowfield_options_list()
  136. */
  137. function workflowfield_field_widget_info_alter(&$info) {
  138. $info['options_select']['field types'][] = 'workflow';
  139. $info['options_buttons']['field types'][] = 'workflow';
  140. }
  141. /**
  142. * Do not implement hook_field_presave(),
  143. * since $nid is needed, but not yet known at this moment.
  144. */
  145. /*
  146. // function workflowfield_field_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
  147. // }
  148. */
  149. /**
  150. * Implements hook_field_insert().
  151. */
  152. function workflowfield_field_insert($entity_type, $entity, $field, $instance, $langcode, &$items) {
  153. return workflowfield_field_update($entity_type, $entity, $field, $instance, $langcode, $items);
  154. }
  155. /**
  156. * Implements hook_field_update().
  157. *
  158. * It is the D7-wrapper for D8-style WorkflowDefaultWidget::submit.
  159. * It is called also from hook_field_insert, since we need $nid to store workflow_node_history.
  160. * We cannot use hook_field_presave, since $nid is not yet known at that moment.
  161. */
  162. function workflowfield_field_update($entity_type, $entity, array $field, $instance, $langcode, &$items) {
  163. $form = array();
  164. $form_state = array();
  165. $field_name = $field['field_name'];
  166. if ($entity_type == 'comment') {
  167. // This happens when we are on an entity's comment.
  168. // We save the field of the node. The comment is saved automatically.
  169. $referenced_entity_type = 'node'; // Comments only exist on nodes.
  170. $referenced_entity_id = $entity->nid;
  171. // Load the node again, since the passed node doesn't contain proper 'type' field.
  172. $referenced_entity = entity_load_single($referenced_entity_type, $referenced_entity_id);
  173. // Normalize the contents of the workflow field.
  174. $items[0]['value'] = _workflow_get_sid_by_items($items);
  175. // Execute the transition upon the node. Afterwards, $items is in form as expected by Field API.
  176. // Remember, we don't know if the transition is scheduled or not.
  177. $widget = new WorkflowTransitionForm($field, $instance, $referenced_entity_type, $referenced_entity);
  178. $widget->submitForm($form, $form_state, $items); // $items is a proprietary D7 parameter.
  179. // // Since we are saving the comment only, we must save the node separately.
  180. // entity_save($referenced_entity_type, $referenced_entity);
  181. }
  182. else {
  183. $widget = new WorkflowTransitionForm($field, $instance, $entity_type, $entity);
  184. $widget->submitForm($form, $form_state, $items); // $items is a proprietary D7 parameter.
  185. }
  186. }
  187. /**
  188. * Implements hook_field_prepare_view().
  189. *
  190. * This hook is needed in the following case:
  191. * - Edit a node with Workflow Field;
  192. * - Change the value of the widget;
  193. * - Click [Preview] button;
  194. * See the "Notice: Undefined index: value in list_field_formatter_view() (line 467+472 of \modules\field\modules\list\list.module)."
  195. * This is because in the Workflow Widget, the data is stored in a 'workflow'
  196. * structure, not a 'value' value.
  197. *
  198. * However, it is a pity that we run this hook in every view :-(
  199. */
  200. function workflowfield_field_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items) {
  201. // I guess Preview mode is only with 1 entity at a time.
  202. if (count ( $items ) !== 1) {
  203. return;
  204. }
  205. foreach ($items as $id => &$item_array) {
  206. foreach ($item_array as $index => &$item) {
  207. if (!isset($item['value'])) {
  208. $item['value'] = isset($item['workflow']['workflow_sid']) ? $item['workflow']['workflow_sid'] : '';
  209. }
  210. }
  211. }
  212. }
  213. /**
  214. * Implements hook_field_delete().
  215. *
  216. * @todo: implement
  217. */
  218. function workflowfield_field_delete($entity_type, $entity, $field, $instance, $langcode, &$items) {
  219. $workflow_field = new WorkflowItem($field, $instance, $entity_type, $entity);
  220. $workflow_field->delete($items);
  221. }
  222. /**
  223. * Implements hook_field_is_empty().
  224. *
  225. * The Workflow field is never empty.
  226. */
  227. function workflowfield_field_is_empty($item, $field) {
  228. // $workflow_field = new WorkflowItem($field, $instance, $entity_type, $entity);
  229. // $workflow_field->isEmpty($item);
  230. return FALSE;
  231. }
  232. /**
  233. * Implements hook_field_delete_field().
  234. *
  235. * @todo: implement functionality from workflow_node_delete().
  236. */
  237. /*
  238. // function workflowfield_field_delete_field($entity_type, $entity, $field, $instance, $langcode, &$items) {
  239. // }
  240. */
  241. /**
  242. * Callback function for list.module formatter.
  243. *
  244. * Returns array of allowed values when using a 'core' list formatter,
  245. * instead of the 'workflow' formatter.
  246. *
  247. * @see list_allowed_values()
  248. */
  249. function workflowfield_allowed_values($field, $instance, $entity_type, $entity) {
  250. $workflow_field = new WorkflowItem($field, $instance, $entity_type, $entity);
  251. return $workflow_field->getAllowedValues();
  252. }
  253. /**
  254. * Implements hook_options_list().
  255. *
  256. * Callback function for the default Options widgets.
  257. *
  258. * @todo: move to a class.
  259. */
  260. function workflowfield_options_list($field, $instance, $entity_type, $entity) {
  261. global $user; // @todo #2287057: OK?
  262. // @todo: Perhaps global user is not always the correct user.
  263. // E.g., on ScheduledTransition->execute()? But this function is mostly used in UI.
  264. $options = array();
  265. if ($entity) {
  266. // Get the allowed new states for the entity's current state.
  267. $field_name = $field['field_name'];
  268. $sid = workflow_node_current_state($entity, $entity_type, $field_name);
  269. $state = workflow_state_load_single($sid);
  270. $options = $state->getOptions($entity_type, $entity, $field_name, $user, FALSE);
  271. }
  272. else {
  273. // $field['settings']['wid'] can be numeric or named.
  274. if ($workflow = workflow_load_single($field['settings']['wid'])) {
  275. // There is no entity, E.g., on the Rules action "Set a data value".
  276. $state = new WorkflowState(array('wid' => $workflow->wid));
  277. $options = $state->getOptions('', NULL, '', $user, FALSE);
  278. }
  279. }
  280. return $options;
  281. }