/**
 * Implement hook_form_submit().
 *
 * Save transitions extra value to workflow option.
 */
function workflow_transition_extra_form_submit($form, $form_state)
{
    if (!empty(array_values($form_state['values']['transitions']))) {
        $form_state['workflow']->options['transitions_extra'] = $form_state['values']['transitions'];
        entity_save('workflow', $form_state['workflow']);
    }
}
 public function save($relationshipName = NULL)
 {
     if (empty($relationshipName)) {
         entity_save(static::$entityType, $this->entity);
     } else {
         $this->get($relationshipName)->save();
     }
 }
Example #3
0
 /**
  * Create a taxonomy term for a given vocabulary.
  *
  * @param string $vocabulary
  *    Vocabulary machine name.
  * @param string $name
  *    Term name.
  * @param string $parent
  *    Eventual parent name.
  *
  * @return object|bool
  *    Return new term object or FALSE.
  */
 public function createTaxonomyTerm($vocabulary, $name, $parent = NULL)
 {
     if ($vocabulary = taxonomy_vocabulary_machine_name_load($vocabulary)) {
         // Exit if term already exists for that vocabulary.
         $term = db_select('taxonomy_term_data', 't')->fields('t', array('tid'))->condition('t.name', $name)->condition('t.vid', $vocabulary->vid)->execute()->fetchAll(PDO::FETCH_COLUMN);
         if ($term) {
             return FALSE;
         }
         $values = array();
         $values['vocabulary_machine_name'] = $vocabulary->machine_name;
         $values['vid'] = $vocabulary->vid;
         $values['name'] = $name;
         if ($parent) {
             $parent_tid = (int) db_select('taxonomy_term_data', 't')->fields('t', array('tid'))->condition('t.name', $parent)->condition('t.vid', $vocabulary->vid)->execute()->fetchAll(PDO::FETCH_COLUMN);
             $values['parent'] = $parent_tid;
         }
         $entity = entity_create('taxonomy_term', $values);
         return entity_save('taxonomy_term', $entity);
     }
     return FALSE;
 }
 public function getProjectRoles($node, $row)
 {
     $field_values = array();
     $roles = array('field_dataset_datamanager_ref' => 'data manager', 'field_dataset_fieldcrew_ref' => 'field crew', 'field_dataset_labcrew_ref' => 'lab crew', 'field_dataset_ext_assoc_ref' => 'associated researcher');
     foreach ($roles as $field => $role) {
         if (!empty($row->{$field}) && ($source_ids = array_filter($row->{$field}))) {
             if ($user_ids = $this->handleSourceMigration('NtlContentPersonNew', $source_ids)) {
                 if (!is_array($user_ids)) {
                     $user_ids = array($user_ids);
                 }
                 foreach ($user_ids as $user_id) {
                     $entity = entity_create('project_role', array('type' => 'project_role', 'language' => $node->language));
                     $wrapper = entity_metadata_wrapper('project_role', $entity);
                     $wrapper->field_project_role = $role;
                     $wrapper->field_related_person = $user_id;
                     entity_save('project_role', $entity);
                     $field_values[] = array('target_id' => $entity->id);
                 }
             }
         }
     }
     return $field_values;
 }
 /**
  * Overrides the 'revert' action, to not delete the workflows.
  *
  * @see https://www.drupal.org/node/2051079
  * @see https://www.drupal.org/node/1043634
  */
 public function applyOperation($op, $entity)
 {
     $label = entity_label($this->entityType, $entity);
     $vars = array('%entity' => $this->entityInfo['label'], '%label' => $label);
     $id = entity_id($this->entityType, $entity);
     $edit_link = l(t('edit'), $this->path . '/manage/' . $id . '/edit');
     switch ($op) {
         case 'revert':
             // Do not delete the workflow, but recreate features_get_default($entity_type, $module);
             // entity_delete($this->entityType, $id);
             $workflow = $entity;
             $entity_type = $this->entityType;
             $funcname = $workflow->module . '_default_' . $this->entityType;
             $defaults = $funcname();
             // No defaults, no processing.
             if (empty($defaults)) {
                 return;
             }
             foreach ($defaults as $name => $entity) {
                 $existing[$name] = workflow_load($name);
                 // If we got an existing entity with the same name, we reuse its entity id.
                 if (isset($existing[$name])) {
                     // Set the original as later reference.
                     $entity->original = $existing[$name];
                     // As we got an ID, the entity is not new.
                     $entity->wid = $entity->original->wid;
                     unset($entity->is_new);
                     // Update the status to be in code.
                     // $entity->status |= ENTITY_IN_CODE;
                     $entity->status = ENTITY_IN_CODE;
                     // We mark it for being in revert mode.
                     $entity->is_reverted = TRUE;
                     entity_save($entity_type, $entity);
                     unset($entity->is_reverted);
                 }
                 // The rest of the defaults is handled by default implementation.
                 // @see entity_defaults_rebuild()
             }
             watchdog($this->entityType, 'Reverted %entity %label to the defaults.', $vars, WATCHDOG_NOTICE, $edit_link);
             return t('Reverted %entity %label to the defaults.', $vars);
         case 'delete':
         case 'import':
         default:
             return parent::applyOperation($op, $entity);
     }
 }
Example #6
0
 /**
  * @When /^I create a new "([^"]*)" with title "([^"]*)" in the site "([^"]*)"$/
  */
 public function iCreateANewEventWithTitle($type, $title, $vsite)
 {
     $tomorrow = time() + 24 * 60 * 60;
     $entity = $this->createEntity($type, $title);
     $entity->field_date['und'][0]['value'] = date('Y-m-d H:i:s');
     $entity->field_date['und'][0]['value2'] = date('Y-m-d H:i:s', $tomorrow);
     $wrapper = entity_metadata_wrapper('node', $entity);
     // Set the group ref
     $nid = FeatureHelp::getNodeId($vsite);
     $wrapper->{OG_AUDIENCE_FIELD}->set(array($nid));
     entity_save('node', $entity);
 }
Example #7
0
 /**
  * Executes the selected operation on the provided data.
  *
  * @param $data
  *   The data to to operate on. An entity or an array of entities.
  * @param $context
  *   An array of related data (selected views rows, etc).
  */
 public function execute($data, array $context)
 {
     $context['entity_type'] = $this->entityType;
     $context['settings'] = $this->getAdminOption('settings', array());
     $context += $this->formOptions;
     $context += $this->operationInfo['parameters'];
     // Actions provided by the Drupal system module require the entity to be
     // present in $context, keyed by entity type.
     if (is_object($data)) {
         $context[$this->entityType] = $data;
     }
     actions_do($this->operationInfo['callback'], $data, $context);
     // The action might need to have its entities saved after execution.
     if (in_array('changes_property', $this->operationInfo['behavior'])) {
         $data = is_array($data) ? $data : array($data);
         foreach ($data as $entity) {
             entity_save($this->entityType, $entity);
         }
     }
 }
 /**
  * Executes the selected operation on the provided entity.
  *
  * @param $entity
  *   The selected entity, or an array of entities, if aggregation is used.
  * @param $context
  *   An array of related data provided by the caller.
  */
 public function execute($entity, array $context)
 {
     $context['entity_type'] = $this->entityType;
     $context['settings'] = $this->getAdminOption('settings', array());
     $context += $this->formOptions;
     $context += $this->operationInfo['parameters'];
     actions_do($this->operationInfo['callback'], $entity, $context);
     // Actions that specify 'changes_property' need to be explicitly saved.
     if (in_array('changes_property', $this->operationInfo['behavior'])) {
         entity_save($this->entityType, $entity);
     }
 }
 /**
  * Inserts cache purge rules via the entity API.
  *
  * @Given the following cache purge rules:
  */
 public function theFollowingCachePurgeRules(TableNode $table)
 {
     $rules = $table->getHash();
     foreach ($rules as $rule) {
         $rule = entity_create('nexteuropa_varnish_cache_purge_rule', array('content_type' => $rule['Content Type'], 'paths' => preg_replace('/\\s*,\\s*/', "\n", $rule['Paths to Purge'])));
         entity_save('nexteuropa_varnish_cache_purge_rule', $rule);
     }
 }
 /**
  * Configures an Integration backend for testing purposes.
  *
  * The backend points to our mocked Integration HTTP server.
  */
 private function setupTestBackend()
 {
     if (!$this->backendWasConfigured) {
         $server = $this->getServer();
         $backend = entity_load_single('integration_backend', 'couchdb');
         $test_backend = clone $backend;
         $test_backend->id = NULL;
         $test_backend->authentication = 'no_authentication';
         $test_backend->machine_name = 'http_mock';
         $test_backend->name = 'HTTP Mock';
         $test_backend->settings['plugin']['backend']['base_url'] = $server->getBaseUrl();
         entity_save('integration_backend', $test_backend);
         $this->backendWasConfigured = TRUE;
         if (!$server->isStarted()) {
             $server->start();
         }
     }
 }
 /**
  * Implement the save function for the entity.
  */
 public function entity_save($entity)
 {
     entity_save('field_collection_item', $entity);
 }
Example #12
0
 public function save()
 {
     $this->is_new = false;
     return \entity_save($this->type, $this->definition);
 }
 /**
  * {@inheritdoc}
  */
 public function submitForm(array &$form, array &$form_state, array &$items)
 {
     // $items is a D7 parameter.
     // @todo: clean this code up. It is the result of gluing code together.
     global $user;
     // @todo #2287057: verify if submit() really is only used for UI. If not, $user must be passed.
     $entity = $this->entity;
     $entity_type = $this->entity_type;
     $field = $this->field;
     $field_name = $field['field_name'];
     // Retrieve the data from the form.
     if (isset($form_state['values']['workflow_field'])) {
         // If $entity filled: We are on a Entity View page or Workflow History Tab page.
         // If $entity empty: We are on an Advanced Action page.
         //      $field = $form_state['values']['workflow_field'];
         //      $instance = $form_state['values']['workflow_instance'];
         //      $entity_type = $form_state['values']['workflow_entity_type'];
         //      $entity = $form_state['values']['workflow_entity'];
         //      $field_name = $field['field_name'];
     } elseif (isset($form_state['triggering_element'])) {
         // We are on an Entity/Node/Comment Form page (add/edit).
         $field_name = $form_state['triggering_element']['#workflow_field_name'];
     } else {
         // We are on an Entity/Comment Form page (add/edit).
     }
     // Determine if the transition is forced.
     // This can be set by a 'workflow_vbo action' in an additional form element.
     $force = isset($form_state['input']['workflow_force']) ? $form_state['input']['workflow_force'] : FALSE;
     // Set language. Multi-language is not supported for Workflow Node.
     $langcode = _workflow_metadata_workflow_get_properties($entity, array(), 'langcode', $entity_type, $field_name);
     if (!$entity) {
         // E.g., on VBO form.
     } elseif ($field_name) {
         // Save the entity, but only if we were not in edit mode.
         // Perhaps there is a better way, but for now we use 'changed' property.
         // Also test for 'is_new'. When Migrating content, the 'changed' property may be set externally.
         // Caveat: Some entities do not have 'changed' property set.
         if (!empty($entity->is_new) || isset($entity->changed) && $entity->changed == REQUEST_TIME) {
             // We are in add/edit mode. No need to save the entity explicitly.
             //        // Add the $form_state to the $items, so we can do a getTransition() later on.
             //        $items[0]['workflow'] = $form_state['input'];
             //        // Create a Transition. The Widget knows if it is scheduled.
             //        $widget = new WorkflowDefaultWidget($field, $instance, $entity_type, $entity);
             //        $new_sid = $widget->submit($form, $form_state, $items, $force);
         } elseif (isset($form_state['input'])) {
             // Save $entity, but only if sid has changed.
             // Use field_attach_update for this? Save always?
             $entity->{$field_name}[$langcode][0]['workflow'] = $form_state['input'];
             // @todo & totest: Save ony the field, not the complete entity.
             // workflow_entity_field_save($entity_type, $entity, $field_name, $langcode, FALSE);
             entity_save($entity_type, $entity);
             return;
             // <---- exit!
         } else {
             // We are saving a node from a comment.
             $entity->{$field_name}[$langcode] = $items;
             // @todo & totest: Save ony the field, not the complete entity.
             // workflow_entity_field_save($entity_type, $entity, $field_name, $langcode, FALSE);
             entity_save($entity_type, $entity);
             return;
             // <---- exit!
         }
     } else {
         // For a Node API form, only contrib fields need to be filled.
         // No updating of the node itself.
         // (Unless we need to record the timestamp.)
         // Add the $form_state to the $items, so we can do a getTransition() later on.
         $items[0]['workflow'] = $form_state['input'];
         //      // Create a Transition. The Widget knows if it is scheduled.
         //      $widget = new WorkflowDefaultWidget($field, $instance, $entity_type, $entity);
         //      $new_sid = $widget->submit($form, $form_state, $items, $force);
     }
     // Extract the data from $items, depending on the type of widget.
     // @todo D8: use MassageFormValues($values, $form, $form_state).
     $old_sid = workflow_node_previous_state($entity, $entity_type, $field_name);
     if (!$old_sid) {
         // At this moment, $old_sid should have a value. If the content does not
         // have a state yet, old_sid contains '(creation)' state. But if the
         // content is not associated to a workflow, old_sid is now 0. This may
         // happen in workflow_vbo, if you assign a state to non-relevant nodes.
         $entity_id = entity_id($entity_type, $entity);
         drupal_set_message(t('Error: content !id has no workflow attached. The data is not saved.', array('!id' => $entity_id)), 'error');
         // The new state is still the previous state.
         $new_sid = $old_sid;
         return $new_sid;
     }
     // Now, save/execute the transition.
     $transition = $this->getTransition($old_sid, $items, $field_name, $user, $form, $form_state);
     // Try to execute the transition. Return $old_sid when error.
     if (!$transition) {
         // This should only happen when testing/developing.
         drupal_set_message(t('Error: the transition from %old_sid to %new_sid could not be generated.'), 'error');
         // The current value is still the previous state.
         $new_sid = $old_sid;
     } elseif ($transition->isScheduled() || $transition->isExecuted()) {
         // A scheduled or executed transition must only be saved to the database.
         // The entity is not changed.
         $force = $force || $transition->isForced();
         $transition->save();
         // The current value is still the previous state.
         $new_sid = $old_sid;
     } elseif (!$transition->isScheduled()) {
         // Now the data is captured in the Transition, and before calling the
         // Execution, restore the default values for Workflow Field.
         // For instance, workflow_rules evaluates this.
         if ($field_name) {
             // $items = array();
             // $items[0]['value'] = $old_sid;
             // $entity->{$field_name}[$transition->language] = $items;
         }
         // It's an immediate change. Do the transition.
         // - validate option; add hook to let other modules change comment.
         // - add to history; add to watchdog
         // Return the new State ID. (Execution may fail and return the old Sid.)
         $force = $force || $transition->isForced();
         $new_sid = $transition->execute($force);
     }
     // The entity is still to be saved, so set to a 'normal' value.
     if ($field_name) {
         $items = array();
         $items[0]['value'] = $new_sid;
         $entity->{$field_name}[$transition->language] = $items;
     }
     return $new_sid;
 }
Example #14
0
function entity_delete($name, &$entity)
{
    $entity->delete = TRUE;
    entity_save($name, $entity);
    $entity = NULL;
}
 /**
  * Function to handle the logic of what to do with expired listings
  * 
  * @param array $in_rets
  * @param array $conid
  * @param drealtyRetsClass $class 
  */
 protected function handle_expired($in_rets, $conid, $class)
 {
     $results = db_select('drealty_listing', 'dl')->fields('dl', array('id', 'rets_key'))->condition('conid', $conid)->condition('class', $class->cid)->condition('active', 1)->execute()->fetchAllAssoc('rets_key');
     $diff = array_diff_key($results, $in_rets);
     foreach ($diff as $item) {
         $wrapper = entity_metadata_wrapper('drealty_listing', $item->id);
         $wrapper->active = 0;
         $wrapper->inactive_date = REQUEST_TIME;
         $property = $wrapper->value();
         entity_save('drealty_listing', $property);
     }
 }
Example #16
0
 /**
  * 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: apparently, in course of time, this is not used anymore. Restore or remove.
     $field_name = $this->field['field_name'];
     // $field['settings']['wid'] can be numeric or named.
     $workflow = workflow_load_single($this->field['settings']['wid']);
     $wid = $workflow->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
         // $todo: 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}[LANGUAGE_NONE]);
         $new_sid = _workflow_get_sid_by_items($items);
         if ($old_sid != $new_sid) {
             $referenced_entity->{$field_name}[LANGUAGE_NONE] = $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(t('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_single($wid)->getCreationSid();
 }
 /**
  * {@inheritdoc}
  */
 public function entity_save($entity)
 {
     entity_save($this->entity_type, $entity);
 }
$_SERVER['HTTP_USER_AGENT'] = 'console';
$modules_to_enable = array('og', 'entity');
// Bootstrap Drupal.
include_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
module_enable(array('og', 'entity'));
// Run cron after installing
drupal_cron_run();
// Create users with ID 2 to 5.
$uids = array();
foreach (range(2, 5) as $i) {
    $values = array('name' => 'og_test_user' . $i, 'mail' => 'og_test_user' . $i . '@example.com', 'pass' => user_password(5), 'status' => 1);
    $account = entity_create('user', $values);
    entity_save('user', $account);
    $uids[] = $account;
}
// Create a group node.
og_create_field(OG_GROUP_FIELD, 'node', 'article');
$values = array('uid' => $uids[0]->uid, 'type' => 'article', 'title' => 'Group node');
$node = entity_create('node', $values);
$node->{OG_GROUP_FIELD}[LANGUAGE_NONE][0]['value'] = 1;
entity_save('node', $node);
// Explicetly set the created timestamp.
$node->{OG_GROUP_FIELD}[LANGUAGE_NONE][0]['created'] = 1000000000;
entity_save('node', $node);
// Assign users to group.
$group = og_get_group('node', $node->nid);
$items = array(1 => OG_STATE_ACTIVE, 2 => OG_STATE_PENDING, 3 => OG_STATE_BLOCKED);
foreach ($items as $key => $state) {
    og_group($group->gid, 'user', $uids[$key], $state);
}
 /**
  * @When /^I create a "(?P<bean_type>(?:[^"]|\\")*)" block with the label "(?P<label>(?:[^"]|\\")*)"$/
  */
 public function imAtAWithTheLabel($bean_type, $label)
 {
     // Create Block.
     $values = array('label' => $label, 'type' => $bean_type);
     $entity = entity_create('bean', $values);
     $saved_entity = entity_save('bean', $entity);
     // Go to bean page.
     // Using vistPath() instead of visit() method since it adds base URL to relative path.
     $this->visitPath('block/' . $entity->delta);
 }
Example #20
0
function user_save(&$user)
{
    entity_save('users', $user);
}