You are here

public function WorkflowItem::update in Workflow 7

Implements hook_field_update() -> FieldItemInterface::update()

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.

1 call to WorkflowItem::update()
WorkflowItem::insert in includes/Field/WorkflowItem.php
Implements hook_field_insert() -> FieldItemInterface::insert()

File

includes/Field/WorkflowItem.php, line 231
Contains workflow\includes\Field\WorkflowItem.

Class

WorkflowItem
Plugin implementation of the 'workflow' field type.

Code

public function update(&$items) {

  // ($entity_type, $entity, $field, $instance, $langcode, &$items) {
  $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 = _workflow_get_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.
    // todo: for now, if nid is set, then it is a node. What happens with other entities?
    $referenced_entity_type = 'node';

    // Comments only exist on nodes.
    $referenced_entities = entity_load($referenced_entity_type, array(
      $nid,
    ));
    $referenced_entity = $referenced_entities[$nid];

    // 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;
      $this
        ->entitySave($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();
}