/** * Form constructor. * * @param array $form * An associative array containing the structure of the form. * @param \Drupal\Core\Form\FormStateInterface $form_state * The current state of the form. * @param \Drupal\Core\Entity\EntityInterface $node * The node being previews * * @return array * The form structure. */ public function buildForm(array $form, FormStateInterface $form_state, EntityInterface $node = NULL) { $view_mode = $node->preview_view_mode; $query_options = $node->isNew() ? array('query' => array('uuid' => $node->uuid())) : array(); $form['backlink'] = array('#type' => 'link', '#title' => $this->t('Back to content editing'), '#url' => $node->isNew() ? Url::fromRoute('node.add', ['node_type' => $node->bundle()]) : $node->urlInfo('edit-form'), '#options' => array('attributes' => array('class' => array('node-preview-backlink'))) + $query_options); $view_mode_options = $this->entityManager->getViewModeOptionsByBundle('node', $node->bundle()); // Unset view modes that are not used in the front end. unset($view_mode_options['rss']); unset($view_mode_options['search_index']); $form['uuid'] = array('#type' => 'value', '#value' => $node->uuid()); $form['view_mode'] = array('#type' => 'select', '#title' => $this->t('View mode'), '#options' => $view_mode_options, '#default_value' => $view_mode, '#attributes' => array('data-drupal-autosubmit' => TRUE)); $form['submit'] = array('#type' => 'submit', '#value' => $this->t('Switch'), '#attributes' => array('class' => array('js-hide'))); return $form; }
/** * {@inheritdoc} */ public function access(EntityInterface $entity, $operation, $langcode = LanguageInterface::LANGCODE_DEFAULT, AccountInterface $account = NULL, $return_as_object = FALSE) { $account = $this->prepareUser($account); if (($return = $this->getCache($entity->uuid(), $operation, $langcode, $account)) !== NULL) { // Cache hit, no work necessary. return $return_as_object ? $return : $return->isAllowed(); } // Invoke hook_entity_access() and hook_ENTITY_TYPE_access(). Hook results // take precedence over overridden implementations of // EntityAccessControlHandler::checkAccess(). Entities that have checks that // need to be done before the hook is invoked should do so by overriding // this method. // We grant access to the entity if both of these conditions are met: // - No modules say to deny access. // - At least one module says to grant access. $access = array_merge($this->moduleHandler()->invokeAll('entity_access', array($entity, $operation, $account, $langcode)), $this->moduleHandler()->invokeAll($entity->getEntityTypeId() . '_access', array($entity, $operation, $account, $langcode))); $return = $this->processAccessHookResults($access); // Also execute the default access check except when the access result is // already forbidden, as in that case, it can not be anything else. if (!$return->isForbidden()) { $return = $return->orIf($this->checkAccess($entity, $operation, $langcode, $account)); } $result = $this->setCache($return, $entity->uuid(), $operation, $langcode, $account); return $return_as_object ? $result : $result->isAllowed(); }
/** * {@inheritdoc} */ public function buildRow(EntityInterface $entity) { /* @var $entity \Drupal\component\Entity\Component */ $row['id'] = $entity->id(); $row['name'] = $this->l($entity->label(), new Url('entity.component.canonical', array('component' => $entity->id()))); $row['uuid'] = $entity->uuid(); return $row + parent::buildRow($entity); }
/** * {@inheritdoc} */ public function buildRow(EntityInterface $entity) { /* @var $entity \Drupal\custom_page\Entity\CustomPage */ $row['id'] = $entity->id(); $row['name'] = $this->l($entity->label(), new Url('entity.custom_page.canonical', array('custom_page' => $entity->id()))); $row['uuid'] = $entity->uuid(); return $row + parent::buildRow($entity); }
/** * {@inheritdoc} */ public function access(EntityInterface $entity, $operation, $langcode = LanguageInterface::LANGCODE_DEFAULT, AccountInterface $account = NULL) { $account = $this->prepareUser($account); if (($access = $this->getCache($entity->uuid(), $operation, $langcode, $account)) !== NULL) { // Cache hit, no work necessary. return $access; } // Invoke hook_entity_access() and hook_ENTITY_TYPE_access(). Hook results // take precedence over overridden implementations of // EntityAccessController::checkAccess(). Entities that have checks that // need to be done before the hook is invoked should do so by overriding // this method. // We grant access to the entity if both of these conditions are met: // - No modules say to deny access. // - At least one module says to grant access. $access = array_merge($this->moduleHandler()->invokeAll('entity_access', array($entity, $operation, $account, $langcode)), $this->moduleHandler()->invokeAll($entity->getEntityTypeId() . '_access', array($entity, $operation, $account, $langcode))); if (($return = $this->processAccessHookResults($access)) === NULL) { // No module had an opinion about the access, so let's the access // controller check create access. $return = (bool) $this->checkAccess($entity, $operation, $langcode, $account); } return $this->setCache($return, $entity->uuid(), $operation, $langcode, $account); }
/** * Saves an entity into the database, from PrivateTempStore. * * @param \Drupal\Core\Entity\EntityInterface $entity * The entity being edited. * * @return \Drupal\Core\Ajax\AjaxResponse * The Ajax response. */ public function entitySave(EntityInterface $entity) { // Take the entity from PrivateTempStore and save in entity storage. // fieldForm() ensures that the PrivateTempStore copy exists ahead. $tempstore = $this->tempStoreFactory->get('quickedit'); $tempstore->get($entity->uuid())->save(); $tempstore->delete($entity->uuid()); // Return information about the entity that allows a front end application // to identify it. $output = array('entity_type' => $entity->getEntityTypeId(), 'entity_id' => $entity->id()); // Respond to client that the entity was saved properly. $response = new AjaxResponse(); $response->addCommand(new EntitySavedCommand($output)); return $response; }
/** * Gets the new entity ID from the location header and tries to read it from * the database. * * @param string $entity_type * Entity type we need to load the entity from DB. * @param \Drupal\Core\Entity\EntityInterface $entity * The entity we want to check that was inserted correctly. * @param array $entity_values * The values of $entity. */ public function assertReadEntityIdFromHeaderAndDb($entity_type, EntityInterface $entity, array $entity_values = array()) { // Get the location from the HTTP response header. $location_url = $this->drupalGetHeader('location'); $url_parts = explode('/', $location_url); $id = end($url_parts); // Get the entity using the ID found. $loaded_entity = \Drupal::entityManager()->getStorage($entity_type)->load($id); $this->assertNotIdentical(FALSE, $loaded_entity, 'The new ' . $entity_type . ' was found in the database.'); $this->assertEqual($entity->uuid(), $loaded_entity->uuid(), 'UUID of created entity is correct.'); // Verify that the field values sent and received from DB are the same. foreach ($entity_values as $property => $value) { $actual_value = $loaded_entity->get($property)->value; $send_value = $entity->get($property)->value; $this->assertEqual($send_value, $actual_value, 'Created property ' . $property . ' expected: ' . $send_value . ', actual: ' . $actual_value); } // Delete the entity loaded from DB. $loaded_entity->delete(); }
/** * {@inheritdoc} */ protected function buildKey(EntityInterface $entity) { return $entity->uuid() . ':' . $entity->_rev->value; }
/** * {@inheritdoc} */ protected function buildKey(EntityInterface $entity) { return $entity->uuid(); }
/** * {@inheritdoc} */ protected function buildValue(EntityInterface $entity) { !($is_new = $entity->isNew()); $revision_id = $is_new ? 0 : $entity->getRevisionId(); // We assign a temporary status to the revision since we are indexing it // pre save. It will be updated post save with the final status. This will // help identifying failures and exception scenarios during entity save. $status = 'indexed'; if (!$is_new && $revision_id) { $status = $entity->_deleted->value ? 'deleted' : 'available'; } return array('entity_type_id' => $entity->getEntityTypeId(), 'entity_id' => $is_new ? 0 : $entity->id(), 'revision_id' => $revision_id, 'uuid' => $entity->uuid(), 'rev' => $entity->_rev->value, 'is_stub' => $entity->_rev->is_stub, 'status' => $status); }
/** * {@inheritdoc} * * @todo Revisit this logic with forward revisions in mind. */ protected function doSave($id, EntityInterface $entity) { if ($entity->_rev->is_stub) { $entity->isDefaultRevision(TRUE); } else { // Enforce new revision if any module messed with it in a hook. $entity->setNewRevision(); // Decide whether or not this is the default revision. if (!$entity->isNew()) { $default_rev = \Drupal::service('entity.index.rev.tree')->getDefaultRevision($entity->uuid()); if ($entity->_rev->value == $default_rev) { $entity->isDefaultRevision(TRUE); } else { $entity->isDefaultRevision(FALSE); } } } return parent::doSave($id, $entity); }