You are here

SimpleMegaMenu.php in Simple Mega Menu 2.0.x

Same filename and directory in other branches
  1. 8 src/Entity/SimpleMegaMenu.php

File

src/Entity/SimpleMegaMenu.php
View source
<?php

namespace Drupal\simple_megamenu\Entity;

use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\RevisionLogEntityTrait;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\RevisionableContentEntityBase;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\user\UserInterface;

/**
 * Defines the Simple mega menu entity.
 *
 * @ingroup simple_megamenu
 *
 * @ContentEntityType(
 *   id = "simple_mega_menu",
 *   label = @Translation("Simple mega menu"),
 *   bundle_label = @Translation("Simple mega menu type"),
 *   handlers = {
 *     "storage" = "Drupal\simple_megamenu\SimpleMegaMenuStorage",
 *     "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
 *     "list_builder" = "Drupal\simple_megamenu\SimpleMegaMenuListBuilder",
 *     "views_data" = "Drupal\simple_megamenu\Entity\SimpleMegaMenuViewsData",
 *     "translation" = "Drupal\simple_megamenu\SimpleMegaMenuTranslationHandler",
 *
 *     "form" = {
 *       "default" = "Drupal\simple_megamenu\Form\SimpleMegaMenuForm",
 *       "add" = "Drupal\simple_megamenu\Form\SimpleMegaMenuForm",
 *       "edit" = "Drupal\simple_megamenu\Form\SimpleMegaMenuForm",
 *       "delete" = "Drupal\simple_megamenu\Form\SimpleMegaMenuDeleteForm",
 *     },
 *     "access" = "Drupal\simple_megamenu\SimpleMegaMenuAccessControlHandler",
 *     "route_provider" = {
 *       "html" = "Drupal\simple_megamenu\SimpleMegaMenuHtmlRouteProvider",
 *     },
 *   },
 *   base_table = "simple_mega_menu",
 *   data_table = "simple_mega_menu_field_data",
 *   revision_table = "simple_mega_menu_revision",
 *   revision_data_table = "simple_mega_menu_field_revision",
 *   translatable = TRUE,
 *   admin_permission = "administer simple mega menu entities",
 *   entity_keys = {
 *     "id" = "id",
 *     "revision" = "vid",
 *     "bundle" = "type",
 *     "label" = "name",
 *     "uuid" = "uuid",
 *     "uid" = "user_id",
 *     "langcode" = "langcode",
 *     "status" = "status",
 *   },
 *   revision_metadata_keys = {
 *     "revision_user" = "revision_uid",
 *     "revision_created" = "revision_timestamp",
 *     "revision_log_message" = "revision_log_message"
 *   },
 *   links = {
 *     "canonical" = "/admin/content/simple_mega_menu/{simple_mega_menu}",
 *     "add-page" = "/admin/content/simple_mega_menu/add",
 *     "add-form" = "/admin/content/simple_mega_menu/add/{simple_mega_menu_type}",
 *     "edit-form" = "/admin/content/simple_mega_menu/{simple_mega_menu}/edit",
 *     "delete-form" = "/admin/content/simple_mega_menu/{simple_mega_menu}/delete",
 *     "version-history" = "/admin/content/simple_mega_menu/{simple_mega_menu}/revisions",
 *     "revision" = "/admin/content/simple_mega_menu/{simple_mega_menu}/revisions/{simple_mega_menu_revision}/view",
 *     "revision_revert" = "/admin/content/simple_mega_menu/{simple_mega_menu}/revisions/{simple_mega_menu_revision}/revert",
 *     "translation_revert" = "/admin/content/simple_mega_menu/{simple_mega_menu}/revisions/{simple_mega_menu_revision}/revert/{langcode}",
 *     "revision_delete" = "/admin/content/simple_mega_menu/{simple_mega_menu}/revisions/{simple_mega_menu_revision}/delete",
 *     "collection" = "/admin/content/simple_mega_menu",
 *   },
 *   bundle_entity_type = "simple_mega_menu_type",
 *   field_ui_base_route = "entity.simple_mega_menu_type.edit_form"
 * )
 */
class SimpleMegaMenu extends RevisionableContentEntityBase implements SimpleMegaMenuInterface {
  use EntityChangedTrait;
  use RevisionLogEntityTrait;

  /**
   * {@inheritdoc}
   */
  public static function preCreate(EntityStorageInterface $storage_controller, array &$values) {
    parent::preCreate($storage_controller, $values);
    $values += [
      'user_id' => \Drupal::currentUser()
        ->id(),
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function preSave(EntityStorageInterface $storage) {
    parent::preSave($storage);
    foreach (array_keys($this
      ->getTranslationLanguages()) as $langcode) {
      $translation = $this
        ->getTranslation($langcode);

      // If no owner has been set explicitly, make the anonymous user the owner.
      if (!$translation
        ->getOwner()) {
        $translation
          ->setOwnerId(0);
      }
    }

    // If no revision author has been set explicitly, make the simple_mega_menu
    // owner the revision author.
    if (!$this
      ->getRevisionUser()) {
      $this
        ->setRevisionUserId($this
        ->getOwnerId());
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getType() {
    return $this
      ->bundle();
  }

  /**
   * {@inheritdoc}
   */
  public function getName() {
    return $this
      ->get('name')->value;
  }

  /**
   * {@inheritdoc}
   */
  public function setName($name) {
    $this
      ->set('name', $name);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getCreatedTime() {
    return $this
      ->get('created')->value;
  }

  /**
   * {@inheritdoc}
   */
  public function setCreatedTime($timestamp) {
    $this
      ->set('created', $timestamp);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getOwner() {
    return $this
      ->get('user_id')->entity;
  }

  /**
   * {@inheritdoc}
   */
  public function getOwnerId() {
    return $this
      ->get('user_id')->target_id;
  }

  /**
   * {@inheritdoc}
   */
  public function setOwnerId($uid) {
    $this
      ->set('user_id', $uid);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function setOwner(UserInterface $account) {
    $this
      ->set('user_id', $account
      ->id());
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function isPublished() {
    return (bool) $this
      ->getEntityKey('status');
  }

  /**
   * {@inheritdoc}
   */
  public function setPublished($published) {
    $this
      ->set('status', $published ? TRUE : FALSE);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getRevisionCreationTime() {
    return $this
      ->get('revision_timestamp')->value;
  }

  /**
   * {@inheritdoc}
   */
  public function setRevisionCreationTime($timestamp) {
    $this
      ->set('revision_timestamp', $timestamp);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getRevisionUser() {
    return $this
      ->get('revision_uid')->entity;
  }

  /**
   * {@inheritdoc}
   */
  public function setRevisionUserId($uid) {
    $this
      ->set('revision_uid', $uid);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
    $fields = parent::baseFieldDefinitions($entity_type);
    $fields['user_id'] = BaseFieldDefinition::create('entity_reference')
      ->setLabel(t('Authored by'))
      ->setDescription(t('The user ID of author of the Simple mega menu entity.'))
      ->setRevisionable(TRUE)
      ->setSetting('target_type', 'user')
      ->setSetting('handler', 'default')
      ->setTranslatable(TRUE)
      ->setDisplayOptions('view', [
      'label' => 'hidden',
      'type' => 'author',
      'weight' => 0,
    ])
      ->setDisplayOptions('form', [
      'type' => 'entity_reference_autocomplete',
      'weight' => 5,
      'settings' => [
        'match_operator' => 'CONTAINS',
        'size' => '60',
        'autocomplete_type' => 'tags',
        'placeholder' => '',
      ],
    ])
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayConfigurable('view', TRUE);
    $fields['name'] = BaseFieldDefinition::create('string')
      ->setLabel(t('Name'))
      ->setDescription(t('The name of the Simple mega menu entity.'))
      ->setRevisionable(TRUE)
      ->setTranslatable(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);
    $fields['status'] = BaseFieldDefinition::create('boolean')
      ->setLabel(t('Publishing status'))
      ->setDescription(t('A boolean indicating whether the Simple mega menu is published.'))
      ->setRevisionable(TRUE)
      ->setTranslatable(TRUE)
      ->setDefaultValue(TRUE)
      ->setDisplayOptions('form', [
      'type' => 'boolean_checkbox',
      'settings' => [
        'display_label' => TRUE,
      ],
      'weight' => 120,
    ])
      ->setDisplayConfigurable('form', TRUE);
    $fields['created'] = BaseFieldDefinition::create('created')
      ->setLabel(t('Created'))
      ->setDescription(t('The time that the entity was created.'));
    $fields['changed'] = BaseFieldDefinition::create('changed')
      ->setLabel(t('Changed'))
      ->setDescription(t('The time that the entity was last edited.'));

    // Add the revision metadata fields.
    $fields += static::revisionLogBaseFieldDefinitions($entity_type);
    return $fields;
  }

}

Classes

Namesort descending Description
SimpleMegaMenu Defines the Simple mega menu entity.