/** * Implements hook_field_delete() -> FieldItemInterface::delete(). */ public function delete($items) { global $user; $entity_type = $this->entity_type; $entity = $this->entity; $entity_id = entity_id($entity_type, $entity); $field_name = $this->field['field_name']; // Delete the record in {workflow_node} - not for Workflow Field. // Use a one-liner for better code analysis when grepping on old code. !$field_name ? workflow_delete_workflow_node_by_nid($entity_id) : NULL; // Add a history record in {workflow_node_history}. // @see drupal.org/node/2165349, comment by Bastlynn: // The reason for this history log upon delete is because Workflow module // has historically been used to track node states and accountability in // business environments where accountability for changes over time is // *absolutely* required. Think banking and/or particularly strict // retention policies for legal reasons. // // However, a deleted nid may be re-used under certain circumstances: // e.g., working with InnoDB or after restart the DB server. // This may cause that old history is associated with a new node. $old_sid = _workflow_get_sid_by_items($items); $new_sid = (int) WORKFLOW_DELETION; $comment = t('Entity deleted.'); $transition = new WorkflowTransition(); $transition->setValues($entity_type, $entity, $field_name, $old_sid, $new_sid, $user->uid, REQUEST_TIME, $comment); $transition->save(); // Delete all records for this node in {workflow_scheduled_transition}. foreach (WorkflowScheduledTransition::load($entity_type, $entity_id, $field_name) as $scheduled_transition) { $scheduled_transition->delete(); } }
/** * Implements hook_field_update() -> FieldItemInterface::update(). * * @todo: in course of time, this is not used anymore... * It is called also from hook_field_insert(), since we need $nid to store {workflow_node_history}. * We cannot use hook_field_presave(), since $nid is not yet known at that moment. * * "Contrary to the old D7 hooks, the methods do not receive the parent entity * "or the langcode of the field values as parameters. If needed, those can be accessed * "by the getEntity() and getLangcode() methods on the Field and FieldItem classes. */ public function update(&$items) { // ($entity_type, $entity, $field, $instance, $langcode, &$items) { // @todo: apparentlly, in course of time, this is not used anymore. Restore or remove. $field_name = $this->field['field_name']; $wid = $this->field['settings']['wid']; $new_state = WorkflowState::load($sid = _workflow_get_sid_by_items($items), $wid); // @todo D8: remove below lines. $entity = $this->entity; $entity_type = $this->entity_type; $entity_id = entity_id($entity_type, $entity); if (!$entity) { // No entity available, we are on the field Settings page - 'default value' field. // This is hidden from the admin, because the default value can be different for every user. } elseif (!$entity_id && $entity_type == 'comment') { // Not possible: a comment on a non-existent node. } elseif ($entity_id && $this->entity_type == 'comment') { // This happens when we are on an entity's comment. $referenced_entity_type = 'node'; // Comments only exist on nodes. $referenced_entity = entity_load_single($referenced_entity_type, $entity_id); // Comments only exist on nodes. // Submit the data. $items is reset by reference to normal value, and is magically saved by the field itself. $form = array(); $form_state = array(); $widget = new WorkflowDefaultWidget($this->field, $this->instance, $referenced_entity_type, $referenced_entity); $widget->submit($form, $form_state, $items); // $items is a proprietary D7 parameter. // Remember: we are on a comment form, so the comment is saved automatically, but the referenced entity is not. // @todo: probably we'd like to do this form within the Widget, but that does not know // wether we are on a comment or a node form. // // Widget::submit() returns the new value in a 'sane' state. // Save the referenced entity, but only is transition succeeded, and is not scheduled. $old_sid = _workflow_get_sid_by_items($referenced_entity->{$field_name}['und']); $new_sid = _workflow_get_sid_by_items($items); if ($old_sid != $new_sid) { $referenced_entity->{$field_name}['und'] = $items; entity_save($referenced_entity_type, $referenced_entity); } } elseif ($this->entity_type != 'comment') { if (isset($items[0]['value'])) { // A 'normal' options.module-widget is used, and $items[0]['value'] is already properly set. } elseif (isset($items[0]['workflow'])) { // The WorkflowDefaultWidget is used. // Submit the data. $items is reset by reference to normal value, and is magically saved by the field itself. $form = array(); $form_state = array(); $widget = new WorkflowDefaultWidget($this->field, $this->instance, $entity_type, $entity); $widget->submit($form, $form_state, $items); // $items is a proprietary D7 parameter. } else { drupal_set_message('error in WorkfowItem->update()', 'error'); } } // A 'normal' node add page. // We should not be here, since we only do inserts after $entity_id is known. // $current_sid = Workflow::load($wid)->getCreationSid(); }