/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { $this->entityManager->getStorage($this->entityRevision->getEntityTypeId())->deleteRevision($this->entityRevision->getRevisionId()); $this->logger('content')->notice('@type: deleted %title revision %revision.', ['@type' => $this->entityRevision->bundle(), '%title' => $this->entityRevision->label(), '%revision' => $this->entityRevision->getRevisionId()]); drupal_set_message(t('@type %title has been deleted', ['@type' => $this->entityRevision->{$this->entityRevision->getEntityType()->getKey('bundle')}->entity->label(), '%title' => $this->entityRevision->label()])); $form_state->setRedirectUrl($this->entityRevision->urlInfo('version-history')); }
/** * Update ancestry and ancestry_plain field of any child that has this as its first parent. * * @var \Drupal\Core\Entity\EntityInterface $discussion * A discussion node that may have had its ancestry updating. */ public function updateChildren(ContentEntityInterface $discussion) { if ($discussion->hasField('field_parents') && $discussion->hasField('field_ancestry') && $discussion->hasField('field_ancestry_plain')) { // If own ancestry has changed, update children's ancestry. $newAncestry = $discussion->field_ancestry->getValue(); $oldAncestry = empty($discussion->original) ? NULL : $discussion->original->field_ancestry->getValue(); $newTitle = $discussion->label(); $oldTitle = empty($discussion->original) ? NULL : $discussion->original->label(); if ($newAncestry !== $oldAncestry || $newTitle !== $oldTitle) { $children = $discussion->field_children->referencedEntities(); $childAncestry = $newAncestry; $childAncestry[] = ["target_id" => $discussion->id()]; foreach ($children as $child) { if ($child->hasField('field_parents') && $child->hasField('field_ancestry') && $child->hasField('field_ancestry_plain')) { // Only update children's ancestry if this is their first parent. if ($child->field_parents->entity->id() === $discussion->id()) { // Only update children's ancestry if child is not already included. // Prevents crazy loops. if (!$this->withinAncestry($child, $childAncestry)) { $child->field_ancestry = $childAncestry; } $child->field_ancestry_plain = $this->makePlain($child->field_ancestry); $child->save(); } } } } } }
/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { // The revision timestamp will be updated when the revision is saved. Keep // the original one for the confirmation message. $this->entityRevision->setNewRevision(); $this->entityRevision->isDefaultRevision(TRUE); if ($this->entityRevision instanceof EntityRevisionLogInterface) { if ($this->entityRevision instanceof TimestampedRevisionInterface) { $original_revision_timestamp = $this->entityRevision->getRevisionCreationTime(); $this->entityRevision->revision_log = t('Copy of the revision from %date.', ['%date' => $this->dateFormatter->format($original_revision_timestamp)]); } else { $this->entityRevision->revision_log = t('Copy of the revision'); } } $this->entityRevision->save(); $this->logger('content')->notice('@type: reverted %title revision %revision.', ['@type' => $this->entityRevision->bundle(), '%title' => $this->entityRevision->label(), '%revision' => $this->entityRevision->getRevisionId()]); drupal_set_message(t('@type %title has been reverted to the revision', ['@type' => $this->entityRevision->{$this->entityRevision->getEntityType()->getKey('bundle')}->entity->label(), '%title' => $this->entityRevision->label()])); $form_state->setRedirectUrl($this->entityRevision->urlInfo('version-history')); }
/** * {@inheritdoc} */ protected function getRevisionDescription(ContentEntityInterface $revision, $is_default = FALSE) { /** @var \Drupal\Core\Entity\ContentEntityInterface|\Drupal\user\EntityOwnerInterface|\Drupal\Core\Entity\RevisionLogInterface $revision */ if ($revision instanceof RevisionLogInterface) { // Use revision link to link to revisions that are not active. $date = $this->dateFormatter->format($revision->getRevisionCreationTime(), 'short'); $link = $revision->toLink($date, 'revision'); // @todo: Simplify this when https://www.drupal.org/node/2334319 lands. $username = ['#theme' => 'username', '#account' => $revision->getRevisionUser()]; $username = $this->renderer->render($username); } else { $link = $revision->toLink($revision->label(), 'revision'); $username = ''; } $markup = ''; if ($revision instanceof RevisionLogInterface) { $markup = $revision->getRevisionLogMessage(); } if ($username) { $template = '{% trans %}{{ date }} by {{ username }}{% endtrans %}{% if message %}<p class="revision-log">{{ message }}</p>{% endif %}'; } else { $template = '{% trans %} {{ date }} {% endtrans %}{% if message %}<p class="revision-log">{{ message }}</p>{% endif %}'; } $column = ['data' => ['#type' => 'inline_template', '#template' => $template, '#context' => ['date' => $link->toString(), 'username' => $username, 'message' => ['#markup' => $markup, '#allowed_tags' => Xss::getHtmlTagList()]]]]; return $column; }
/** * The title getter. * * @return string * The title of the event. */ public function getTitle() { return $this->entity->label(); }
/** * Check an entity value after reload. * * @param $entity_type_id * @param \Drupal\Core\Entity\ContentEntityInterface $entity * @param $field * @param $value */ protected function checkEntityValue($entity_type_id, ContentEntityInterface $entity, $field, $value) { $storage = \Drupal::entityTypeManager()->getStorage($entity_type_id); $storage->resetCache([$entity->id()]); $updated_entity = $storage->load($entity->id()); $this->assertEqual($updated_entity->get($field)->value, 1, $entity->label() . " $field = $value"); }
/** * Generate a filename from the entity. * * @param \Drupal\Core\Entity\ContentEntityInterface $entity * The content entity to generate the filename. * * @return string * The cleaned filename from the entity label. */ protected function generateFilename(ContentEntityInterface $entity) { $filename = preg_replace("/[^A-Za-z0-9 ]/", '', $entity->label()); // If for some bizarre reason there isn't a valid character in the entity // title or the entity doesn't provide a label then we use the entity type. if (!$filename) { $filename = $entity->getEntityTypeId(); } return $filename . '.pdf'; }
protected function getRevisionDescription(ContentEntityInterface $revision, $is_current = FALSE) { /** @var \Drupal\Core\Entity\ContentEntityInterface|\Drupal\user\EntityOwnerInterface $revision */ if ($revision instanceof EntityOwnerInterface) { $username = ['#theme' => 'username', '#account' => $revision->getOwner()]; } else { $username = ''; } if ($revision instanceof ExpandedEntityRevisionInterface) { // Use revision link to link to revisions that are not active. $date = $this->dateFormatter()->format($revision->getRevisionCreationTime(), 'short'); if (!$is_current) { $link = $this->l($date, $revision->urlInfo('revision')); } else { $link = $revision->link($date); } } else { $link = $revision->link($revision->label(), 'revision'); } $markup = ''; if ($revision instanceof EntityRevisionLogInterface) { $markup = $revision->getRevisionLog(); } if ($username) { $template = '{% trans %}{{ date }} by {{ username }}{% endtrans %}{% if message %}<p class="revision-log">{{ message }}</p>{% endif %}'; } else { $template = '{% trans %} {{ date }} {% endtrans %}{% if message %}<p class="revision-log">{{ message }}</p>{% endif %}'; } $column = ['data' => ['#type' => 'inline_template', '#template' => $template, '#context' => ['date' => $link, 'username' => $this->renderer()->renderPlain($username), 'message' => ['#markup' => $markup, '#allowed_tags' => Xss::getHtmlTagList()]]]]; return $column; }
/** * Builds a table row for overview form. * * @param array ContentEntityInterface $entity * Data needed to build the list row. * @param array $bundles * The array of bundles. * * @return array */ public function overviewRow(ContentEntityInterface $entity, array $bundles) { $label = $entity->label() ?: $this->t('@type: @id', array('@type' => $entity->getEntityTypeId(), '@id' => $entity->id())); // Get existing translations and current job items for the entity // to determine translation statuses $translations = $entity->getTranslationLanguages(); $source_lang = $entity->language()->getId(); $current_job_items = tmgmt_job_item_load_latest('content', $entity->getEntityTypeId(), $entity->id(), $source_lang); $row = array('id' => $entity->id(), 'title' => $entity->hasLinkTemplate('canonical') ? $entity->toLink($label, 'canonical')->toString() : ($entity->label() ?: $entity->id())); if (count($bundles) > 1) { $row['bundle'] = isset($bundles[$entity->bundle()]) ? $bundles[$entity->bundle()] : t('Unknown'); } // Load entity translation specific data. $manager = \Drupal::service('content_translation.manager'); foreach (\Drupal::languageManager()->getLanguages() as $langcode => $language) { $translation_status = 'current'; if ($langcode == $source_lang) { $translation_status = 'original'; } elseif (!isset($translations[$langcode])) { $translation_status = 'missing'; } elseif ($translation = $entity->getTranslation($langcode)) { $metadata = $manager->getTranslationMetadata($translation); if ($metadata->isOutdated()) { $translation_status = 'outofdate'; } } $build = $this->buildTranslationStatus($translation_status, isset($current_job_items[$langcode]) ? $current_job_items[$langcode] : NULL); $row['langcode-' . $langcode] = ['data' => \Drupal::service('renderer')->render($build), 'class' => array('langstatus-' . $langcode)]; } return $row; }