/** * {@inheritdoc} */ protected function checkAccess(EntityInterface $node, $operation, $langcode, AccountInterface $account) { /** @var \Drupal\node\NodeInterface $node */ /** @var \Drupal\node\NodeInterface $translation */ $translation = $node->getTranslation($langcode); // Fetch information from the node object if possible. $status = $translation->isPublished(); $uid = $translation->getOwnerId(); // Check if authors can view their own unpublished nodes. if ($operation === 'view' && !$status && $account->hasPermission('view own unpublished content')) { if ($account->id() != 0 && $account->id() == $uid) { return TRUE; } } // If no module specified either allow or deny, we fall back to the // node_access table. if (($grants = $this->grantStorage->access($node, $operation, $langcode, $account)) !== NULL) { return $grants; } // If no modules implement hook_node_grants(), the default behavior is to // allow all users to view published nodes, so reflect that here. if ($operation === 'view') { return $status; } }
/** * Returns an Ajax response to render a text field without transformation filters. * * @param \Drupal\Core\Entity\EntityInterface $entity * The entity of which a formatted text field is being rerendered. * @param string $field_name * The name of the (formatted text) field that that is being rerendered * @param string $langcode * The name of the language for which the formatted text field is being * rerendered. * @param string $view_mode_id * The view mode the formatted text field should be rerendered in. * * @return \Drupal\Core\Ajax\AjaxResponse * The Ajax response. */ public function getUntransformedText(EntityInterface $entity, $field_name, $langcode, $view_mode_id) { $response = new AjaxResponse(); // Direct text editing is only supported for single-valued fields. $field = $entity->getTranslation($langcode)->{$field_name}; $editable_text = check_markup($field->value, $field->format, $langcode, array(FilterInterface::TYPE_TRANSFORM_REVERSIBLE, FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE)); $response->addCommand(new GetUntransformedTextCommand($editable_text)); return $response; }
/** * {@inheritdoc} */ public function getTranslationFromContext(EntityInterface $entity, $langcode = NULL, $context = array()) { $translation = $entity; if ($entity instanceof TranslatableInterface) { if (empty($langcode)) { $langcode = $this->languageManager->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)->getId(); } // Retrieve language fallback candidates to perform the entity language // negotiation. $context['data'] = $entity; $context += array('operation' => 'entity_view', 'langcode' => $langcode); $candidates = $this->languageManager->getFallbackCandidates($context); // Ensure the default language has the proper language code. $default_language = $entity->getUntranslated()->language(); $candidates[$default_language->getId()] = LanguageInterface::LANGCODE_DEFAULT; // Return the most fitting entity translation. foreach ($candidates as $candidate) { if ($entity->hasTranslation($candidate)) { $translation = $entity->getTranslation($candidate); break; } } } return $translation; }
/** * Returns the translation object to use to retrieve the translated values. * * @param \Drupal\Core\Entity\EntityInterface $entity * The entity being tested. * @param string $langcode * The language code identifying the translation to be retrieved. * * @return \Drupal\Core\TypedData\TranslatableInterface * The translation object to act on. */ protected function getTranslation(EntityInterface $entity, $langcode) { return $entity->getTranslation($langcode); }
/** * {@inheritdoc} */ protected function getFormSubmitAction(EntityInterface $entity, $langcode) { if ($entity->getTranslation($langcode)->isPublished()) { return t('Save and keep published') . $this->getFormSubmitSuffix($entity, $langcode); } else { return t('Save and keep unpublished') . $this->getFormSubmitSuffix($entity, $langcode); } }
/** * {@inheritdoc} */ public function retranslate(EntityInterface $entity, $langcode = NULL) { $updated_langcode = !empty($langcode) ? $langcode : $entity->language()->getId(); foreach ($entity->getTranslationLanguages() as $langcode => $language) { $this->manager->getTranslationMetadata($entity->getTranslation($langcode))->setOutdated($langcode != $updated_langcode); } }
/** * Saves values of configurable fields for an entity. * * @param \Drupal\Core\Entity\EntityInterface $entity * The entity. * @param bool $update * TRUE if the entity is being updated, FALSE if it is being inserted. */ protected function saveFieldItems(EntityInterface $entity, $update = TRUE) { $vid = $entity->getRevisionId(); $id = $entity->id(); $bundle = $entity->bundle(); $entity_type = $entity->getEntityTypeId(); $default_langcode = $entity->getUntranslated()->language()->id; $translation_langcodes = array_keys($entity->getTranslationLanguages()); if (!isset($vid)) { $vid = $id; } foreach ($this->entityManager->getFieldDefinitions($entity_type, $bundle) as $field_name => $field_definition) { $storage_definition = $field_definition->getFieldStorageDefinition(); if (!$this->usesDedicatedTable($storage_definition)) { continue; } $table_name = static::_fieldTableName($storage_definition); $revision_name = static::_fieldRevisionTableName($storage_definition); // Delete and insert, rather than update, in case a value was added. if ($update) { // Only overwrite the field's base table if saving the default revision // of an entity. if ($entity->isDefaultRevision()) { $this->database->delete($table_name)->condition('entity_id', $id)->execute(); } $this->database->delete($revision_name)->condition('entity_id', $id)->condition('revision_id', $vid)->execute(); } // Prepare the multi-insert query. $do_insert = FALSE; $columns = array('entity_id', 'revision_id', 'bundle', 'delta', 'langcode'); foreach ($storage_definition->getColumns() as $column => $attributes) { $columns[] = static::_fieldColumnName($storage_definition, $column); } $query = $this->database->insert($table_name)->fields($columns); $revision_query = $this->database->insert($revision_name)->fields($columns); $langcodes = $field_definition->isTranslatable() ? $translation_langcodes : array($default_langcode); foreach ($langcodes as $langcode) { $delta_count = 0; $items = $entity->getTranslation($langcode)->get($field_name); $items->filterEmptyItems(); foreach ($items as $delta => $item) { // We now know we have someting to insert. $do_insert = TRUE; $record = array('entity_id' => $id, 'revision_id' => $vid, 'bundle' => $bundle, 'delta' => $delta, 'langcode' => $langcode); foreach ($storage_definition->getColumns() as $column => $attributes) { $column_name = static::_fieldColumnName($storage_definition, $column); // Serialize the value if specified in the column schema. $record[$column_name] = !empty($attributes['serialize']) ? serialize($item->{$column}) : $item->{$column}; } $query->values($record); $revision_query->values($record); if ($storage_definition->getCardinality() != FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED && ++$delta_count == $storage_definition->getCardinality()) { break; } } } // Execute the query if we have values to insert. if ($do_insert) { // Only overwrite the field's base table if saving the default revision // of an entity. if ($entity->isDefaultRevision()) { $query->execute(); } $revision_query->execute(); } } }
/** * {@inheritdoc} */ protected function checkAccess(EntityInterface $node, $operation, $langcode, AccountInterface $account) { /** @var \Drupal\node\NodeInterface $node */ /** @var \Drupal\node\NodeInterface $translation */ $translation = $node->getTranslation($langcode); // Fetch information from the node object if possible. $status = $translation->isPublished(); $uid = $translation->getOwnerId(); // Check if authors can view their own unpublished nodes. if ($operation === 'view' && !$status && $account->hasPermission('view own unpublished content') && $account->isAuthenticated() && $account->id() == $uid) { return AccessResult::allowed()->cachePerRole()->cachePerUser()->cacheUntilEntityChanges($node); } // If no module specified either ALLOW or KILL, we fall back to the // node_access table. $grants = $this->grantStorage->access($node, $operation, $langcode, $account); if ($grants->isAllowed() || $grants->isForbidden()) { return $grants; } // If no modules implement hook_node_grants(), the default behavior is to // allow all users to view published nodes, so reflect that here. if ($operation === 'view') { return AccessResult::allowedIf($status)->cacheUntilEntityChanges($node); } // No opinion. return AccessResult::neutral(); }
/** * {@inheritdoc} */ protected function checkAccess(EntityInterface $node, $operation, $langcode, AccountInterface $account) { /** @var \Drupal\node\NodeInterface $node */ /** @var \Drupal\node\NodeInterface $translation */ $translation = $node->getTranslation($langcode); // Fetch information from the node object if possible. $status = $translation->isPublished(); $uid = $translation->getOwnerId(); // Check if authors can view their own unpublished nodes. if ($operation === 'view' && !$status && $account->hasPermission('view own unpublished content') && $account->isAuthenticated() && $account->id() == $uid) { return AccessResult::allowed()->cachePerPermissions()->cachePerUser()->cacheUntilEntityChanges($node); } // Evaluate node grants. return $this->grantStorage->access($node, $operation, $langcode, $account); }
/** * Initializes the translation form state. * * @param \Drupal\Core\Form\FormStateInterface $form_state * @param \Drupal\Core\Entity\EntityInterface $host */ protected function initIsTranslating(FormStateInterface $form_state, EntityInterface $host) { if ($this->isTranslating != NULL) { return; } $this->isTranslating = FALSE; if (!$host->isTranslatable()) { return; } if (!$host->getEntityType()->hasKey('default_langcode')) { return; } $default_langcode_key = $host->getEntityType()->getKey('default_langcode'); if (!$host->hasField($default_langcode_key)) { return; } if (!empty($form_state->get('content_translation'))) { // Adding a language through the ContentTranslationController. $this->isTranslating = TRUE; } if ($host->hasTranslation($form_state->get('langcode')) && $host->getTranslation($form_state->get('langcode'))->get($default_langcode_key)->value == 0) { // Editing a translation. $this->isTranslating = TRUE; } }