<?php
namespace Drupal\paragraphs_library\Entity;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Entity\EditorialContentEntityBase;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\paragraphs\ParagraphInterface;
use Drupal\paragraphs_library\LibraryItemInterface;
use Drupal\user\UserInterface;
class LibraryItem extends EditorialContentEntityBase implements LibraryItemInterface {
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
$fields['label'] = BaseFieldDefinition::create('string')
->setLabel(t('Label'))
->setTranslatable(TRUE)
->setRevisionable(TRUE)
->setSettings([
'max_length' => 255,
])
->setDisplayOptions('view', [
'type' => 'string',
'weight' => 0,
'label' => 'hidden',
])
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => -5,
])
->setRequired(TRUE)
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['paragraphs'] = BaseFieldDefinition::create('entity_reference_revisions')
->setLabel(t('Paragraphs'))
->setRevisionable(TRUE)
->setSetting('target_type', 'paragraph')
->setSetting('handler', 'default')
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'entity_reference_revisions_entity_view',
'weight' => 0,
])
->setDisplayOptions('form', [
'type' => 'paragraphs',
'weight' => 0,
])
->setRequired(TRUE)
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Authored on'))
->setDescription(t('The time that the library item was created.'))
->setRevisionable(TRUE)
->setTranslatable(TRUE)
->setDisplayOptions('form', array(
'region' => 'hidden',
'weight' => 0,
))
->setDisplayConfigurable('form', TRUE);
$fields['changed'] = BaseFieldDefinition::create('changed')
->setLabel(t('Changed'))
->setDescription(t('The time that the library item was last edited.'))
->setRevisionable(TRUE)
->setTranslatable(TRUE);
$fields['uid'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Authored by'))
->setDescription(t('The user ID of the library item author.'))
->setRevisionable(TRUE)
->setSetting('target_type', 'user')
->setSetting('handler', 'default')
->setDefaultValueCallback('Drupal\\paragraphs_library\\Entity\\LibraryItem::getCurrentUserId')
->setTranslatable(TRUE)
->setDisplayOptions('form', array(
'region' => 'hidden',
'weight' => 0,
))
->setDisplayConfigurable('form', TRUE);
$fields['status']
->setDisplayOptions('form', [
'type' => 'boolean_checkbox',
'settings' => [
'display_label' => TRUE,
],
'weight' => 3,
])
->setDisplayConfigurable('form', TRUE);
return $fields;
}
public static function getCurrentUserId() {
return array(
\Drupal::currentUser()
->id(),
);
}
public function preSave(EntityStorageInterface $storage) {
if (!$this
->getRevisionUser()) {
$this
->setRevisionUserId($this
->getOwnerId());
}
parent::preSave($storage);
}
public function getOwner() {
return $this
->get('uid')->entity;
}
public function getOwnerId() {
return $this
->get('uid')->target_id;
}
public function setOwnerId($uid) {
$this
->set('uid', $uid);
return $this;
}
public function setOwner(UserInterface $account) {
$this
->set('uid', $account
->id());
return $this;
}
public function preSaveRevision(EntityStorageInterface $storage, \stdClass $record) {
parent::preSaveRevision($storage, $record);
if (!$this
->isNewRevision() && isset($this->original) && (!isset($record->revision_log) || $record->revision_log === '')) {
$record->revision_log = $this->original->revision_log->value;
}
$new_revision = $this
->isNewRevision();
if (!$new_revision && isset($this->original) && (!isset($record->revision_log) || $record->revision_log === '')) {
$record->revision_log = $this->original
->getRevisionLogMessage();
}
if ($new_revision && (!isset($record->revision_created) || empty($record->revision_created))) {
$record->revision_created = \Drupal::time()
->getRequestTime();
}
}
public static function createFromParagraph(ParagraphInterface $paragraph) {
if (!$paragraph
->getParagraphType()
->getThirdPartySetting('paragraphs_library', 'allow_library_conversion', FALSE)) {
throw new \Exception(sprintf('%s cannot be converted to library item per configuration', $paragraph
->bundle()));
}
$paragraph = $paragraph
->getUntranslated();
if (\Drupal::hasService('replicate.replicator')) {
$duplicate_paragraph = \Drupal::getContainer()
->get('replicate.replicator')
->replicateEntity($paragraph);
}
else {
$duplicate_paragraph = $paragraph
->createDuplicate();
}
$duplicate_paragraph
->save();
$library_item = static::create([
'paragraphs' => $duplicate_paragraph,
'langcode' => $paragraph
->language()
->getId(),
]);
if ($library_item
->hasField('moderation_state')) {
$library_item
->set('moderation_state', 'published');
}
foreach ($duplicate_paragraph
->getTranslationLanguages() as $langcode => $language) {
if (!$library_item
->hasTranslation($langcode)) {
$library_item
->addTranslation($langcode, $library_item
->toArray());
}
$library_item
->getTranslation($langcode)
->set('label', static::buildLabel($duplicate_paragraph
->getTranslation($langcode)));
}
return $library_item;
}
protected static function buildLabel(ParagraphInterface $paragraph) {
$summary = $paragraph
->getSummaryItems([
'show_behavior_summary' => FALSE,
]);
$summary = Unicode::truncate(implode(', ', $summary['content']), 50);
return $paragraph
->getParagraphType()
->label() . ': ' . $summary;
}
public function getCreatedTime() {
return $this
->get('created')->value;
}
public function setCreatedTime($timestamp) {
$this
->set('created', $timestamp);
return $this;
}
protected function urlRouteParameters($rel) {
$uri_route_parameters = parent::urlRouteParameters($rel);
if ($rel === 'revision-revert' || $rel === 'revision-delete') {
$uri_route_parameters['paragraphs_library_item_revision'] = $this
->getRevisionId();
}
return $uri_route_parameters;
}
}