workflowfield.field.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. if (isset($info['i18n_list_default'])) {
  129. $info['i18n_list_default']['field types'][] = 'workflow';
  130. }
  131. }
  132. /**
  133. * Implements hook_field_widget_info_alter().
  134. *
  135. * The module does not implement widgets of its own, but reuses the
  136. * widgets defined in options.module.
  137. *
  138. * @see workflowfield_options_list()
  139. */
  140. function workflowfield_field_widget_info_alter(&$info) {
  141. $info['options_select']['field types'][] = 'workflow';
  142. $info['options_buttons']['field types'][] = 'workflow';
  143. }
  144. /**
  145. * Do not implement hook_field_presave(),
  146. * since $nid is needed, but not yet known at this moment.
  147. */
  148. /*
  149. // function workflowfield_field_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
  150. // }
  151. */
  152. /**
  153. * Implements hook_field_insert().
  154. */
  155. function workflowfield_field_insert($entity_type, $entity, $field, $instance, $langcode, &$items) {
  156. return workflowfield_field_update($entity_type, $entity, $field, $instance, $langcode, $items);
  157. }
  158. /**
  159. * Implements hook_field_update().
  160. *
  161. * It is the D7-wrapper for D8-style WorkflowDefaultWidget::submit.
  162. * It is called also from hook_field_insert, since we need $nid to store workflow_node_history.
  163. * We cannot use hook_field_presave, since $nid is not yet known at that moment.
  164. */
  165. function workflowfield_field_update($entity_type, $entity, array $field, $instance, $langcode, &$items) {
  166. $form = array();
  167. $form_state = array();
  168. $field_name = $field['field_name'];
  169. if ($entity_type == 'comment') {
  170. // This happens when we are on an entity's comment.
  171. // We save the field of the node. The comment is saved automatically.
  172. $referenced_entity_type = 'node'; // Comments only exist on nodes.
  173. $referenced_entity_id = $entity->nid;
  174. // Load the node again, since the passed node doesn't contain proper 'type' field.
  175. $referenced_entity = entity_load_single($referenced_entity_type, $referenced_entity_id);
  176. // Normalize the contents of the workflow field.
  177. $items[0]['value'] = _workflow_get_sid_by_items($items);
  178. // Execute the transition upon the node. Afterwards, $items is in form as expected by Field API.
  179. // Remember, we don't know if the transition is scheduled or not.
  180. $widget = new WorkflowTransitionForm($field, $instance, $referenced_entity_type, $referenced_entity);
  181. $widget->submitForm($form, $form_state, $items); // $items is a proprietary D7 parameter.
  182. // // Since we are saving the comment only, we must save the node separately.
  183. // entity_save($referenced_entity_type, $referenced_entity);
  184. }
  185. else {
  186. $widget = new WorkflowTransitionForm($field, $instance, $entity_type, $entity);
  187. $widget->submitForm($form, $form_state, $items); // $items is a proprietary D7 parameter.
  188. }
  189. }
  190. /**
  191. * Implements hook_field_prepare_view().
  192. *
  193. * This hook is needed in the following case:
  194. * - Edit a node with Workflow Field;
  195. * - Change the value of the widget;
  196. * - Click [Preview] button;
  197. * See the "Notice: Undefined index: value in list_field_formatter_view() (line 467+472 of \modules\field\modules\list\list.module)."
  198. * This is because in the Workflow Widget, the data is stored in a 'workflow'
  199. * structure, not a 'value' value.
  200. *
  201. * However, it is a pity that we run this hook in every view :-(
  202. */
  203. function workflowfield_field_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items) {
  204. // I guess Preview mode is only with 1 entity at a time.
  205. if (count ( $items ) !== 1) {
  206. return;
  207. }
  208. foreach ($items as $id => &$item_array) {
  209. foreach ($item_array as $index => &$item) {
  210. if (!isset($item['value'])) {
  211. $item['value'] = isset($item['workflow']['workflow_sid']) ? $item['workflow']['workflow_sid'] : '';
  212. }
  213. }
  214. }
  215. }
  216. /**
  217. * Implements hook_field_delete().
  218. *
  219. * @todo: implement
  220. */
  221. function workflowfield_field_delete($entity_type, $entity, $field, $instance, $langcode, &$items) {
  222. $workflow_field = new WorkflowItem($field, $instance, $entity_type, $entity);
  223. $workflow_field->delete($items);
  224. }
  225. /**
  226. * Implements hook_field_is_empty().
  227. *
  228. * The Workflow field is never empty.
  229. */
  230. function workflowfield_field_is_empty($item, $field) {
  231. // $workflow_field = new WorkflowItem($field, $instance, $entity_type, $entity);
  232. // $workflow_field->isEmpty($item);
  233. return FALSE;
  234. }
  235. /**
  236. * Implements hook_field_delete_field().
  237. *
  238. * @todo: implement functionality from workflow_node_delete().
  239. */
  240. /*
  241. // function workflowfield_field_delete_field($entity_type, $entity, $field, $instance, $langcode, &$items) {
  242. // }
  243. */
  244. /**
  245. * Callback function for list.module formatter.
  246. *
  247. * Returns array of allowed values when using a 'core' list formatter,
  248. * instead of the 'workflow' formatter.
  249. *
  250. * @see list_allowed_values()
  251. */
  252. function workflowfield_allowed_values($field, $instance, $entity_type, $entity) {
  253. $workflow_field = new WorkflowItem($field, $instance, $entity_type, $entity);
  254. return $workflow_field->getAllowedValues();
  255. }
  256. /**
  257. * Implements hook_options_list().
  258. *
  259. * Callback function for the default Options widgets.
  260. *
  261. * @todo: move to a class.
  262. */
  263. function workflowfield_options_list($field, $instance, $entity_type, $entity) {
  264. global $user; // @todo #2287057: OK?
  265. // @todo: Perhaps global user is not always the correct user.
  266. // E.g., on ScheduledTransition->execute()? But this function is mostly used in UI.
  267. $options = array();
  268. if ($entity) {
  269. // Get the allowed new states for the entity's current state.
  270. $field_name = $field['field_name'];
  271. $sid = workflow_node_current_state($entity, $entity_type, $field_name);
  272. $state = workflow_state_load_single($sid);
  273. $options = $state->getOptions($entity_type, $entity, $field_name, $user, FALSE);
  274. }
  275. else {
  276. // $field['settings']['wid'] can be numeric or named.
  277. if ($workflow = workflow_load_single($field['settings']['wid'])) {
  278. // There is no entity, E.g., on the Rules action "Set a data value".
  279. $state = new WorkflowState(array('wid' => $workflow->wid));
  280. $options = $state->getOptions('', NULL, '', $user, FALSE);
  281. }
  282. }
  283. return $options;
  284. }
  285. /**
  286. * Implements hook_i18n_field_info().
  287. */
  288. function workflowfield_i18n_field_info() {
  289. $info['workflow'] = array(
  290. 'translate_options' => 'workflowfield_i18n_field_translate_allowed_values',
  291. );
  292. return $info;
  293. }
  294. /**
  295. * Returns the array of translated allowed values for a workflow (list) field.
  296. *
  297. * @param $field
  298. * The field definition.
  299. *
  300. * @return
  301. * The array of allowed values. Keys of the array are the raw stored values
  302. * (number or text), values are the translated, sanitized labels.
  303. */
  304. function workflowfield_i18n_field_translate_allowed_values($field) {
  305. // State labels are already translated and sanitized.
  306. $workflow_states = workflow_get_workflow_state_names($field['settings']['wid'], $grouped = FALSE, $all = TRUE);
  307. return $workflow_states;
  308. }