page_layouts-1.0.1/src/Entity/Layout.php
src/Entity/Layout.php
<?php
declare(strict_types=1);
namespace Drupal\page_layouts\Entity;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\RevisionableContentEntityBase;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\page_layouts\LayoutInterface;
use Drupal\user\EntityOwnerTrait;
/**
* Defines the layout entity class.
*
* @ContentEntityType(
* id = "page_layouts_layout",
* label = @Translation("Layout"),
* label_collection = @Translation("Layouts"),
* label_singular = @Translation("layout"),
* label_plural = @Translation("layouts"),
* label_count = @PluralTranslation(
* singular = "@count layouts",
* plural = "@count layouts",
* ),
* handlers = {
* "list_builder" = "Drupal\page_layouts\LayoutListBuilder",
* "views_data" = "Drupal\views\EntityViewsData",
* "form" = {
* "add" = "Drupal\page_layouts\Form\LayoutForm",
* "edit" = "Drupal\page_layouts\Form\LayoutForm",
* "delete" = "Drupal\Core\Entity\ContentEntityDeleteForm",
* "delete-multiple-confirm" = "Drupal\Core\Entity\Form\DeleteMultipleForm",
* "revision-delete" = \Drupal\Core\Entity\Form\RevisionDeleteForm::class,
* "revision-revert" = \Drupal\Core\Entity\Form\RevisionRevertForm::class,
* },
* "route_provider" = {
* "html" = "Drupal\Core\Entity\Routing\AdminHtmlRouteProvider",
* "revision" = \Drupal\Core\Entity\Routing\RevisionHtmlRouteProvider::class,
* },
* },
* base_table = "page_layouts_layout",
* data_table = "page_layouts_layout_field_data",
* revision_table = "page_layouts_layout_revision",
* revision_data_table = "page_layouts_layout_field_revision",
* show_revision_ui = TRUE,
* translatable = TRUE,
* admin_permission = "administer page_layouts_layout",
* entity_keys = {
* "id" = "id",
* "revision" = "revision_id",
* "langcode" = "langcode",
* "label" = "label",
* "uuid" = "uuid",
* "owner" = "uid",
* },
* revision_metadata_keys = {
* "revision_user" = "revision_uid",
* "revision_created" = "revision_timestamp",
* "revision_log_message" = "revision_log",
* },
* links = {
* "collection" = "/admin/content/layout",
* "add-form" = "/layout/add",
* "canonical" = "/layout/{page_layouts_layout}",
* "edit-form" = "/layout/{page_layouts_layout}/edit",
* "delete-form" = "/layout/{page_layouts_layout}/delete",
* "delete-multiple-form" = "/admin/content/layout/delete-multiple",
* "revision" = "/layout/{page_layouts_layout}/revision/{page_layouts_layout_revision}/view",
* "revision-delete-form" = "/layout/{page_layouts_layout}/revision/{page_layouts_layout_revision}/delete",
* "revision-revert-form" = "/layout/{page_layouts_layout}/revision/{page_layouts_layout_revision}/revert",
* "version-history" = "/layout/{page_layouts_layout}/revisions",
* },
* field_ui_base_route = "entity.page_layouts_layout.settings",
* )
*/
final class Layout extends RevisionableContentEntityBase implements LayoutInterface {
use EntityChangedTrait;
use EntityOwnerTrait;
/**
* Helper function to populate the layouts list.
*
* @return array
* Dropdown options.
*/
public static function getLayouts() {
$layouts = [];
$layoutManager = \Drupal::service('plugin.manager.layouts');
$plugin_definitions = $layoutManager->getDefinitions();
foreach ($plugin_definitions as $plugin_id => $definition) {
$layouts[$plugin_id] = (string) $definition["label"];
}
return $layouts;
}
/**
* {@inheritdoc}
*/
public function preSave(EntityStorageInterface $storage): void {
parent::preSave($storage);
if (!$this->getOwnerId()) {
// If no owner has been set explicitly, make the anonymous user the owner.
$this->setOwnerId(0);
}
}
/**
* {@inheritdoc}
*/
public static function baseFieldDefinitions(EntityTypeInterface $entity_type): array {
$fields = parent::baseFieldDefinitions($entity_type);
$fields['label'] = BaseFieldDefinition::create('string')
->setRevisionable(TRUE)
->setTranslatable(TRUE)
->setLabel(t('Label'))
->setRequired(TRUE)
->setSetting('max_length', 255)
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => -5,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'string',
'weight' => -5,
])
->setDisplayConfigurable('view', TRUE);
$fields['weight'] = BaseFieldDefinition::create('integer')
->setLabel(t('Weight'))
->setDescription(t('The order of the templates on the page. 1, 2, 3 etc..'))
->setRevisionable(TRUE)
->setSettings([
'max_length' => 50,
'text_processing' => 0,
])
->setDefaultValue('')
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'string',
'weight' => -4,
])
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => -4,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE)
->setRequired(TRUE);
$fields['layout'] = BaseFieldDefinition::create('list_string')
->setLabel(t('Layout'))
->setDescription(t('Choose the layout that will display the content'))
->setRevisionable(TRUE)
->setSettings([
'allowed_values' => self::getLayouts(),
])
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'string',
'weight' => 1,
])
->setDisplayOptions('form', [
'type' => 'options_select',
'weight' => 1,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE)
->setRequired(TRUE);
$fields['image_1'] = BaseFieldDefinition::create('image')
->setLabel(t('Image 1'))
->setDescription(t('An image placed in a template either center or to the left when in a template with two cells.'))
->setRevisionable(TRUE)
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'image',
'weight' => 2,
])
->setDisplayOptions('form', [
'type' => 'image_image',
'weight' => 2,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['image_2'] = BaseFieldDefinition::create('image')
->setLabel(t('Image 2'))
->setDescription(t('An image placed in a template to the right in a template with two cells. Leave empty if the template does not need two images.'))
->setRevisionable(TRUE)
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'image',
'weight' => 3,
])
->setDisplayOptions('form', [
'type' => 'image_image',
'weight' => 3,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['content_1'] = BaseFieldDefinition::create('string_long')
->setLabel(t('Content 1'))
->setDescription(t('The text placed in a template either center or to the left when in a template with two cells.'))
->setRevisionable(TRUE)
->setSettings([
'max_length' => 2048,
'text_processing' => 0,
])
->setDefaultValue('')
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'string',
'weight' => 8,
])
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => 8,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE)
->setRequired(FALSE);
$fields['content_2'] = BaseFieldDefinition::create('string_long')
->setLabel(t('Content 2'))
->setDescription(t('The text placed in a template to the right in a template with two cells.'))
->setRevisionable(TRUE)
->setSettings([
'max_length' => 2048,
'text_processing' => 0,
])
->setDefaultValue('')
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'string',
'weight' => 9,
])
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => 9,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE)
->setRequired(FALSE);
$fields['status'] = BaseFieldDefinition::create('boolean')
->setRevisionable(TRUE)
->setLabel(t('Status'))
->setDefaultValue(TRUE)
->setSetting('on_label', 'Enabled')
->setDisplayOptions('form', [
'type' => 'boolean_checkbox',
'settings' => [
'display_label' => FALSE,
],
'weight' => 10,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayOptions('view', [
'type' => 'boolean',
'label' => 'above',
'weight' => 10,
'settings' => [
'format' => 'enabled-disabled',
],
])
->setDisplayConfigurable('view', TRUE);
$fields['uid'] = BaseFieldDefinition::create('entity_reference')
->setRevisionable(TRUE)
->setTranslatable(TRUE)
->setLabel(t('Author'))
->setSetting('target_type', 'user')
->setDefaultValueCallback(self::class . '::getDefaultEntityOwner')
->setDisplayOptions('form', [
'type' => 'entity_reference_autocomplete',
'settings' => [
'match_operator' => 'CONTAINS',
'size' => 60,
'placeholder' => '',
],
'weight' => 15,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'author',
'weight' => 15,
])
->setDisplayConfigurable('view', TRUE);
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Authored on'))
->setTranslatable(TRUE)
->setDescription(t('The time that the layout was created.'))
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'timestamp',
'weight' => 20,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayOptions('form', [
'type' => 'datetime_timestamp',
'weight' => 20,
])
->setDisplayConfigurable('view', TRUE);
$fields['changed'] = BaseFieldDefinition::create('changed')
->setLabel(t('Changed'))
->setTranslatable(TRUE)
->setDescription(t('The time that the layout was last edited.'));
return $fields;
}
}
