Example #1
0
 /**
  * {@inheritdoc}
  */
 public static function preCreate(EntityStorageInterface $storage, array &$values)
 {
     parent::preCreate($storage, $values);
     if (empty($values['type'])) {
         $values['type'] = $storage->getEntityTypeId();
     }
 }
Example #2
0
 /**
  * {@inheritdoc}
  */
 public function preSave(EntityStorageInterface $storage)
 {
     parent::preSave($storage);
     // If no owner has been set explicitly, make the current user the owner.
     if (!$this->getOwner()) {
         $this->setOwnerId($this->getCurrentUserId());
     }
 }
Example #3
0
 /**
  * {@inheritdoc}
  */
 public static function baseFieldDefinitions(EntityTypeInterface $entity_type)
 {
     $fields = parent::baseFieldDefinitions($entity_type);
     $fields['name'] = BaseFieldDefinition::create('string')->setLabel(t('Name'))->setDescription(t('The name of the test entity.'))->setTranslatable(TRUE)->setSetting('max_length', 32)->setDisplayOptions('view', array('label' => 'hidden', 'type' => 'string', 'weight' => -5))->setDisplayOptions('form', array('type' => 'string_textfield', 'weight' => -5));
     $fields['created'] = BaseFieldDefinition::create('created')->setLabel(t('Authored on'))->setDescription(t('Time the entity was created'))->setTranslatable(TRUE);
     $fields['user_id'] = BaseFieldDefinition::create('entity_reference')->setLabel(t('User ID'))->setDescription(t('The ID of the associated user.'))->setSetting('target_type', 'user')->setSetting('handler', 'default')->setDefaultValue(array(0 => array('target_id' => 1)))->setTranslatable(TRUE)->setDisplayOptions('form', array('type' => 'entity_reference_autocomplete', 'weight' => -1, 'settings' => array('match_operator' => 'CONTAINS', 'size' => '60', 'placeholder' => '')));
     return $fields;
 }
Example #4
0
 /**
  * {@inheritdoc}
  */
 public static function preCreate(EntityStorageInterface $storage_controller, array &$values)
 {
     parent::preCreate($storage_controller, $values);
     $values += array('user_id' => \Drupal::currentUser()->id());
     if (isset($values['issue_type'])) {
         $values['issue_type'] = \Drupal::service('checkstyle.issue.nodemapper')->getCheckstyleTypeId($values['issue_type']);
     }
     return FALSE;
 }
Example #5
0
 /**
  * {@inheritdoc}
  */
 public static function postLoad(EntityStorageInterface $storage, array &$orders)
 {
     parent::postLoad($storage, $orders);
     foreach ($orders as $id => $order) {
         $order->products = \Drupal::entityManager()->getStorage('uc_order_product')->loadByProperties(['order_id' => $id]);
         // Load line items... has to be last after everything has been loaded.
         $order->line_items = $order->getLineItems();
     }
 }
Example #6
0
 /**
  * {@inheritdoc}
  */
 public function preSaveRevision(EntityStorageInterface $storage, \stdClass $record)
 {
     parent::preSaveRevision($storage, $record);
     if (!$this->isNewRevision() && isset($this->original) && (!isset($record->revision_log) || $record->revision_log === '')) {
         // If we are updating an existing entity without adding a new
         // revision and the user did not supply a revision log, keep the existing
         // one.
         $record->revision_log = $this->original->getRevisionLog();
     }
 }
Example #7
0
 /**
  * {@inheritdoc}
  */
 public function postSave(EntityStorageInterface $storage, $update = TRUE)
 {
     parent::postSave($storage, $update);
     // Only change the parents if a value is set, keep the existing values if
     // not.
     if (isset($this->parent->target_id)) {
         $storage->deleteTermHierarchy(array($this->id()));
         $storage->updateTermHierarchy($this);
     }
 }
 /**
  * @covers ::label
  */
 public function testLabel()
 {
     // Make a mock with one method that we use as the entity's label callback.
     // We check that it is called, and that the entity's label is the callback's
     // return value.
     $callback_label = $this->randomMachineName();
     $callback_container = $this->getMock(get_class());
     $callback_container->expects($this->once())->method(__FUNCTION__)->will($this->returnValue($callback_label));
     $this->entityType->expects($this->once())->method('getLabelCallback')->will($this->returnValue(array($callback_container, __FUNCTION__)));
     $this->assertSame($callback_label, $this->entity->label());
 }
 /**
  * Tests denormalization of an entity.
  */
 public function testDenormalize()
 {
     $normalized = $this->serializer->normalize($this->entity);
     foreach (array('json', 'xml') as $type) {
         $denormalized = $this->serializer->denormalize($normalized, $this->entityClass, $type, array('entity_type' => 'entity_test_mulrev'));
         $this->assertTrue($denormalized instanceof $this->entityClass, SafeMarkup::format('Denormalized entity is an instance of @class', array('@class' => $this->entityClass)));
         $this->assertIdentical($denormalized->getEntityTypeId(), $this->entity->getEntityTypeId(), 'Expected entity type found.');
         $this->assertIdentical($denormalized->bundle(), $this->entity->bundle(), 'Expected entity bundle found.');
         $this->assertIdentical($denormalized->uuid(), $this->entity->uuid(), 'Expected entity UUID found.');
     }
 }
Example #10
0
 /**
  * {@inheritdoc}
  */
 public static function postLoad(EntityStorageInterface $storage, array &$items)
 {
     foreach ($items as $item) {
         $item->product = uc_product_load_variant($item->nid->target_id, $item->data->first()->toArray());
         if ($item->product) {
             $item->title = $item->product->label();
             $item->model = $item->product->model;
             $item->cost = $item->product->cost->value;
             $item->price = $item->product->price;
             $item->weight = $item->product->weight->value;
             $item->weight_units = $item->product->weight->units;
         }
         $item->module = $item->data->module;
     }
     parent::postLoad($storage, $items);
 }
Example #11
0
 /**
  * {@inheritdoc}
  */
 public function postSave(EntityStorageInterface $storage, $update = TRUE)
 {
     parent::postSave($storage, $update);
     // If no order number has been set explicitly, set it to the order id.
     if (!$this->getOrderNumber()) {
         $this->setOrderNumber($this->id());
         $this->save();
     }
     // Ensure there's a back-reference on each line item.
     foreach ($this->getLineItems() as $line_item) {
         if ($line_item->order_id->isEmpty()) {
             $line_item->order_id = $this->id();
             $line_item->save();
         }
     }
 }
Example #12
0
 /**
  * {@inheritdoc}
  */
 public static function postDelete(EntityStorageInterface $storage, array $entities)
 {
     parent::postDelete($storage, $entities);
     $uids = array_keys($entities);
     \Drupal::service('user.data')->delete(NULL, $uids);
 }
Example #13
0
 /**
  * {@inheritdoc}
  */
 public static function preDelete(EntityStorageInterface $storage, array $entities)
 {
     parent::preDelete($storage, $entities);
     /** @var \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager */
     $menu_link_manager = \Drupal::service('plugin.manager.menu.link');
     foreach ($entities as $menu_link) {
         /** @var \Drupal\menu_link_content\Entity\MenuLinkContent $menu_link */
         $menu_link_manager->removeDefinition($menu_link->getPluginId(), FALSE);
     }
 }
Example #14
0
File: Node.php Project: brstde/gap1
 /**
  * {@inheritdoc}
  */
 public function access($operation = 'view', AccountInterface $account = NULL, $return_as_object = FALSE)
 {
     if ($operation == 'create') {
         return parent::access($operation, $account, $return_as_object);
     }
     return \Drupal::entityManager()->getAccessControlHandler($this->entityTypeId)->access($this, $operation, $this->prepareLangcode(), $account, $return_as_object);
 }
Example #15
0
 /**
  * {@inheritdoc}
  */
 public static function postDelete(EntityStorageInterface $storage, array $entities)
 {
     parent::postDelete($storage, $entities);
     if (\Drupal::moduleHandler()->moduleExists('block')) {
         // Make sure there are no active blocks for these feeds.
         $ids = \Drupal::entityQuery('block')->condition('plugin', 'aggregator_feed_block')->condition('settings.feed', array_keys($entities))->execute();
         if ($ids) {
             $block_storage = \Drupal::entityManager()->getStorage('block');
             $block_storage->delete($block_storage->loadMultiple($ids));
         }
     }
 }
Example #16
0
 /**
  * Gets the absolute URI of an entity.
  *
  * @param \Drupal\Core\Entity\ContentEntityBase $entity
  *   The entity for which to generate the URI.
  *
  * @return string
  *   The absolute URI.
  */
 protected function getAbsoluteUri($entity)
 {
     return $entity->url('canonical', array('absolute' => TRUE));
 }
Example #17
0
 /**
  * {@inheritdoc}
  */
 public function postSave(EntityStorageInterface $storage, $update = TRUE)
 {
     parent::postSave($storage, $update);
     // Ensure there's a back-reference on each product variation.
     foreach ($this->variations as $item) {
         $variation = $item->entity;
         if ($variation->product_id->isEmpty()) {
             $variation->product_id = $this->id();
             $variation->save();
         }
     }
 }
 /**
  * Saves the entity.
  * Mostly, you'd better use WorkflowTransitionInterface::execute();
  *
  * {@inheritdoc}
  */
 public function save()
 {
     // return parent::save();
     // Avoid custom actions for subclass WorkflowScheduledTransition.
     if ($this->isScheduled()) {
         return parent::save();
     }
     if ($this->getEntityTypeId() != 'workflow_transition') {
         return parent::save();
     }
     $transition = $this;
     $entity_type = $transition->getTargetEntityTypeId();
     $entity_id = $transition->getTargetEntityId();
     $field_name = $transition->getFieldName();
     // Remove any scheduled state transitions.
     foreach (WorkflowScheduledTransition::loadMultipleByProperties($entity_type, [$entity_id], [], $field_name) as $scheduled_transition) {
         /* @var WorkflowTransitionInterface $scheduled_transition */
         $scheduled_transition->delete();
     }
     // Check for no transition.
     if ($this->getFromSid() == $this->getToSid()) {
         if (!$this->getComment()) {
             // Write comment into history though.
             return SAVED_UPDATED;
         }
     }
     $hid = $this->id();
     if (!$hid) {
         // Insert the transition. Make sure it hasn't already been inserted.
         // @todo: Allow a scheduled transition per revision.
         // @todo: Allow a state per language version (langcode).
         $found_transition = self::loadByProperties($entity_type, $entity_id, [], $field_name);
         if ($found_transition && $found_transition->getTimestamp() == REQUEST_TIME && $found_transition->getToSid() == $this->getToSid()) {
             return SAVED_UPDATED;
         } else {
             return parent::save();
         }
     } else {
         // Update the transition.
         return parent::save();
     }
 }
 /**
  * {@inheritdoc}
  */
 public function postCreate(EntityStorageInterface $storage)
 {
     parent::postCreate($storage);
     // Set the uid field if there is a user with the same email.
     $user_ids = \Drupal::entityQuery('user')->condition('mail', $this->getMail())->execute();
     if (!empty($user_ids)) {
         $this->setUserId(array_pop($user_ids));
     }
     // Copy values for shared fields from existing user.
     if (\Drupal::config('simplenews.settings')->get('subscriber.sync_fields') && ($user = $this->getUser())) {
         foreach ($this->getUserSharedFields($user) as $field_name) {
             $this->set($field_name, $user->get($field_name)->getValue());
         }
     }
 }
Example #20
0
 /**
  * {@inheritdoc}
  */
 public static function preDelete(EntityStorageInterface $storage, array $entities)
 {
     parent::preDelete($storage, $entities);
     foreach ($entities as $entity) {
         // Delete all remaining references to this file.
         $file_usage = \Drupal::service('file.usage')->listUsage($entity);
         if (!empty($file_usage)) {
             foreach ($file_usage as $module => $usage) {
                 \Drupal::service('file.usage')->delete($entity, $module);
             }
         }
         // Delete the actual file. Failures due to invalid files and files that
         // were already deleted are logged to watchdog but ignored, the
         // corresponding file entity will be deleted.
         file_unmanaged_delete($entity->getFileUri());
     }
 }
 /**
  * Overrides \Drupal\Core\Entity\Entity::createDuplicate().
  */
 public function createDuplicate()
 {
     $duplicate = parent::createDuplicate();
     $duplicate->revision_id->value = NULL;
     $duplicate->id->value = NULL;
     return $duplicate;
 }
Example #22
0
 /**
  * {@inheritdoc}
  */
 public function delete()
 {
     foreach ($this->getInstances() as $instance) {
         $instance->delete();
     }
     parent::delete();
 }
 /**
  * @covers ::set
  */
 public function testSet()
 {
     // Exercise set(), check if it returns $this
     $this->assertSame($this->entity, $this->entity->set('id', 0));
 }
Example #24
0
 /**
  * {@inheritdoc}
  */
 public static function baseFieldDefinitions(EntityTypeInterface $entity_type)
 {
     /** @var \Drupal\Core\Field\BaseFieldDefinition[] $fields */
     $fields = parent::baseFieldDefinitions($entity_type);
     $fields['id']->setDescription(t('The ID of the shortcut.'));
     $fields['uuid']->setDescription(t('The UUID of the shortcut.'));
     $fields['shortcut_set']->setLabel(t('Shortcut set'))->setDescription(t('The bundle of the shortcut.'));
     $fields['langcode']->setDescription(t('The language code of the shortcut.'));
     $fields['title'] = BaseFieldDefinition::create('string')->setLabel(t('Name'))->setDescription(t('The name of the shortcut.'))->setRequired(TRUE)->setTranslatable(TRUE)->setSetting('max_length', 255)->setDisplayOptions('form', array('type' => 'string_textfield', 'weight' => -10, 'settings' => array('size' => 40)));
     $fields['weight'] = BaseFieldDefinition::create('integer')->setLabel(t('Weight'))->setDescription(t('Weight among shortcuts in the same shortcut set.'));
     $fields['link'] = BaseFieldDefinition::create('link')->setLabel(t('Path'))->setDescription(t('The location this shortcut points to.'))->setRequired(TRUE)->setSettings(array('link_type' => LinkItemInterface::LINK_INTERNAL, 'title' => DRUPAL_DISABLED))->setDisplayOptions('form', array('type' => 'link_default', 'weight' => 0))->setDisplayConfigurable('form', TRUE);
     return $fields;
 }
 public function preSave(EntityStorageInterface $storage)
 {
     parent::preSave($storage);
     // TODO: Change the autogenerated stub
 }
Example #26
0
 /**
  * {@inheritdoc}
  */
 public function label()
 {
     $label = parent::label();
     if (empty($label)) {
         $label = t('Nameless #@id', ['@id' => $this->id()]);
     }
     return $label;
 }
Example #27
0
 /**
  * {@inheritdoc}
  */
 public function postSave(EntityStorageInterface $storage, $update = TRUE)
 {
     parent::postSave($storage, $update);
     // Entity::postSave() calls Entity::invalidateTagsOnSave(), which only
     // handles the regular cases. The Item entity has one special case: a newly
     // created Item is *also* associated with a Feed, so we must invalidate the
     // associated Feed's cache tag.
     if (!$update) {
         Cache::invalidateTags($this->getCacheTagsToInvalidate());
     }
 }
 /**
  * {@inheritdoc}
  */
 public function postSave(EntityStorageInterface $storage, $update = TRUE)
 {
     parent::postSave($storage, $update);
     // Entity::postSave() calls Entity::invalidateTagsOnSave(), which only
     // handles the regular cases. The Shortcut entity has one special case: a
     // newly created shortcut is *also* added to a shortcut set, so we must
     // invalidate the associated shortcut set's cache tag.
     if (!$update) {
         Cache::invalidateTags($this->getCacheTag());
     }
 }
Example #29
0
 /**
  * {@inheritdoc}
  */
 public static function preCreate(EntityStorageInterface $storage_controller, array &$values) {
   parent::preCreate($storage_controller, $values);
   $values += array(
     'user_id' => \Drupal::currentUser()->id(),
   );
 }
Example #30
0
 /**
  * {@inheritdoc}
  */
 public function referencedEntities()
 {
     $referenced_entities = parent::referencedEntities();
     if ($this->getCommentedEntityId()) {
         $referenced_entities[] = $this->getCommentedEntity();
     }
     return $referenced_entities;
 }