View source
<?php
namespace Drupal\modal_page\Entity;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\modal_page\ModalPageInterface;
use Drupal\user\UserInterface;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Component\Utility\Xss;
use Drupal\modal_page\Helper\ModalPageFieldHelper;
class Modal extends ContentEntityBase implements ModalPageInterface {
use EntityChangedTrait;
public function preSave(EntityStorageInterface $storage) {
parent::preSave($storage);
$pages = $this
->get('pages')->value;
$pages = explode(PHP_EOL, $pages);
foreach ($pages as $page) {
$path = $page;
if ($path != '<front>') {
$path = Xss::filter($path);
}
$path = trim($path);
$aliasPath[] = \Drupal::service('path_alias.manager')
->getPathByAlias($path);
}
$pages = implode(PHP_EOL, $aliasPath);
$this
->set('pages', $pages);
}
public static function preCreate(EntityStorageInterface $storage_controller, array &$values) {
parent::preCreate($storage_controller, $values);
$values += [
'user_id' => \Drupal::currentUser()
->id(),
];
}
public function getCreatedTime() {
return $this
->get('created')->value;
}
public function getChangedTime() {
return $this
->get('changed')->value;
}
public function getOwner() {
return $this
->get('user_id')->entity;
}
public function getOwnerId() {
return $this
->get('user_id')->target_id;
}
public function setOwnerId($uid) {
$this
->set('user_id', $uid);
return $this;
}
public function setOwner(UserInterface $account) {
$this
->set('user_id', $account
->id());
return $this;
}
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$modalPageFieldHelper = new ModalPageFieldHelper();
$fields['id'] = BaseFieldDefinition::create('integer')
->setLabel(t('ID'))
->setDescription(t('The ID of the Modal entity.'))
->setReadOnly(TRUE);
$fields['uuid'] = BaseFieldDefinition::create('uuid')
->setLabel(t('UUID'))
->setDescription(t('The UUID of the Modal entity.'))
->setReadOnly(TRUE);
$fields['title'] = BaseFieldDefinition::create('string')
->setLabel(t('Title'))
->setRequired(TRUE)
->setSettings([
'max_length' => 255,
'text_processing' => 0,
])
->setDefaultValue(NULL)
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'string',
'weight' => -6,
])
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => -6,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['body'] = BaseFieldDefinition::create('text_long')
->setLabel(t('Body'))
->setRequired(TRUE)
->setSettings([
'max_length' => 255,
'text_processing' => 0,
])
->setDefaultValue(NULL)
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'text_default',
'weight' => -5,
])
->setDisplayOptions('form', [
'type' => 'text_textarea',
'weight' => -5,
'settings' => [
'rows' => 11,
],
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['type'] = BaseFieldDefinition::create('list_string')
->setLabel(t('Modal By'))
->setRequired(TRUE)
->setSettings([
'allowed_values' => [
'page' => 'Page',
'parameter' => 'Parameter',
],
])
->setDefaultValue('page')
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'string',
'weight' => -5,
])
->setDisplayOptions('form', [
'type' => 'options_select',
'weight' => -5,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$pages_description = t("One per line. The '*' character is a wildcard. An example path is /admin/* for every admin pages. Leave in blank to show in all pages. @front_key@ is used to front page", [
'@front_key@' => '<front>',
]);
$fields['pages'] = BaseFieldDefinition::create('string_long')
->setLabel(t('Pages'))
->setDescription($pages_description)
->setSettings([
'max_length' => 255,
'text_processing' => 0,
])
->setDefaultValue(NULL)
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'string',
'weight' => -5,
])
->setDisplayOptions('form', [
'type' => 'string_textarea',
'weight' => -5,
'settings' => [
'rows' => 4,
],
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$parameters_description = t('Parameters for the Modal appear');
$parameters_description .= '<ul>';
$parameters_description .= '<li>' . t('Enter one parameters per line') . '</li>';
$parameters_description .= '<li>' . t('An example path is welcome for show in this parameter') . '</li>';
$parameters_description .= '<li>' . t('In URL should be /page?modal=welcome') . '</li>';
$parameters_description .= '</ul>';
$fields['parameters'] = BaseFieldDefinition::create('string_long')
->setLabel(t('Parameters'))
->setDescription($parameters_description)
->setSettings([
'max_length' => 255,
'text_processing' => 0,
])
->setDefaultValue(NULL)
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'string',
'weight' => -5,
])
->setDisplayOptions('form', [
'type' => 'string_textarea',
'weight' => -5,
'settings' => [
'rows' => 4,
],
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['auto_open'] = BaseFieldDefinition::create('boolean')
->setLabel(t("Auto Open"))
->setDefaultValue(TRUE)
->setDisplayConfigurable('form', TRUE)
->setDisplayOptions('form', [
'type' => 'boolean_checkbox',
'settings' => [
'display_label' => TRUE,
],
'weight' => -5,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['open_modal_on_element_click'] = BaseFieldDefinition::create('string')
->setLabel(t('Open this modal clicking on this element'))
->setDescription(t('Example: <b>@example_class@</b>. Default is <b>@default_class@</b>', [
'@example_class@' => '.open-modal-welcome',
'@default_class@' => '.open-modal-page',
]))
->setRequired(FALSE)
->setSettings([
'max_length' => 255,
'text_processing' => 0,
])
->setDefaultValue(NULL)
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'string',
'weight' => -5,
])
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => -5,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['roles'] = $modalPageFieldHelper
->getFieldRole();
$fields['enable_dont_show_again_option'] = BaseFieldDefinition::create('boolean')
->setLabel(t("Enable option <b>Don't show again</b>"))
->setDefaultValue(TRUE)
->setDisplayConfigurable('form', TRUE)
->setDisplayOptions('form', [
'type' => 'boolean_checkbox',
'settings' => [
'display_label' => TRUE,
],
'weight' => -5,
]);
$fields['dont_show_again_label'] = $modalPageFieldHelper
->getFieldDontShowAgainLabel();
$fields['close_modal_esc_key'] = $modalPageFieldHelper
->getFieldCloseModalEscKey();
$fields['close_modal_clicking_outside'] = $modalPageFieldHelper
->getFieldCloseClickingOutside();
$fields['published'] = BaseFieldDefinition::create('boolean')
->setLabel(t("Published"))
->setDefaultValue(TRUE)
->setDisplayConfigurable('form', TRUE)
->setDisplayOptions('form', [
'type' => 'boolean_checkbox',
'settings' => [
'display_label' => TRUE,
],
'weight' => -5,
]);
$fields['delay_display'] = BaseFieldDefinition::create('integer')
->setLabel(t('Delay to display'))
->setDescription(t('Value in seconds.'))
->setDefaultValue(0)
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'string',
'weight' => -5,
])
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => -5,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['modal_size'] = BaseFieldDefinition::create('list_string')
->setLabel(t('Modal Size'))
->setDescription(t('Select the size of your modal.'))
->setRequired(TRUE)
->setSettings([
'allowed_values' => [
'modal-sm' => 'Small',
'modal-md' => 'Medium',
'modal-lg' => 'Large',
],
])
->setDefaultValue('modal-md')
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'string',
'weight' => -5,
])
->setDisplayOptions('form', [
'type' => 'options_select',
'weight' => -5,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['ok_label_button'] = BaseFieldDefinition::create('string')
->setLabel(t('OK Label Button'))
->setDefaultValue(t('OK'))
->setDescription(t('If blank the value will be <b>OK</b>'))
->setSettings([
'max_length' => 255,
'text_processing' => 0,
])
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'string',
'weight' => -5,
])
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => -5,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['langcode'] = BaseFieldDefinition::create('language')
->setLabel(t('Language code'))
->setDescription(t('The language code of Modal entity.'));
$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.'));
return $fields;
}
}