View source
<?php
namespace Drupal\redirect\Entity;
use Drupal\Component\Utility\Crypt;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\link\LinkItemInterface;
class Redirect extends ContentEntityBase {
public static function generateHash($source_path, array $source_query, $language) {
$hash = [
'source' => mb_strtolower($source_path),
'language' => $language,
];
if (!empty($source_query)) {
$hash['source_query'] = $source_query;
}
redirect_sort_recursive($hash, 'ksort');
return Crypt::hashBase64(serialize($hash));
}
public static function preCreate(EntityStorageInterface $storage_controller, array &$values) {
$values += [
'type' => 'redirect',
];
}
public function preSave(EntityStorageInterface $storage_controller) {
$this
->set('hash', Redirect::generateHash($this->redirect_source->path, (array) $this->redirect_source->query, $this
->get('language')->value));
}
public function setLanguage($language) {
$this
->set('language', $language);
}
public function setStatusCode($status_code) {
$this
->set('status_code', $status_code);
}
public function getStatusCode() {
return $this
->get('status_code')->value;
}
public function setCreated($datetime) {
$this
->set('created', $datetime);
}
public function getCreated() {
return $this
->get('created')->value;
}
public function setSource($path, array $query = []) {
$this->redirect_source
->set(0, [
'path' => ltrim($path, '/'),
'query' => $query,
]);
}
public function getSource() {
return $this
->get('redirect_source')
->get(0)
->getValue();
}
public function getSourceUrl() {
return $this
->get('redirect_source')
->get(0)
->getUrl()
->toString();
}
public function getSourcePathWithQuery() {
$path = '/' . $this
->get('redirect_source')->path;
if ($this
->get('redirect_source')->query) {
$path .= '?' . UrlHelper::buildQuery($this
->get('redirect_source')->query);
}
return $path;
}
public function getRedirect() {
return $this
->get('redirect_redirect')
->get(0)
->getValue();
}
public function setRedirect($url, array $query = [], array $options = []) {
$uri = $url . ($query ? '?' . UrlHelper::buildQuery($query) : '');
$external = UrlHelper::isValid($url, TRUE);
$uri = $external ? $url : 'internal:/' . ltrim($uri, '/');
$this->redirect_redirect
->set(0, [
'uri' => $uri,
'options' => $options,
]);
}
public function getRedirectUrl() {
return $this
->get('redirect_redirect')
->get(0)
->getUrl();
}
public function getRedirectOptions() {
return $this
->get('redirect_redirect')->options;
}
public function getRedirectOption($key, $default = NULL) {
$options = $this
->getRedirectOptions();
return isset($options[$key]) ? $options[$key] : $default;
}
public function getHash() {
return $this
->get('hash')->value;
}
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields['rid'] = BaseFieldDefinition::create('integer')
->setLabel(t('Redirect ID'))
->setDescription(t('The redirect ID.'))
->setReadOnly(TRUE);
$fields['uuid'] = BaseFieldDefinition::create('uuid')
->setLabel(t('UUID'))
->setDescription(t('The record UUID.'))
->setReadOnly(TRUE);
$fields['hash'] = BaseFieldDefinition::create('string')
->setLabel(t('Hash'))
->setSetting('max_length', 64)
->setDescription(t('The redirect hash.'));
$fields['type'] = BaseFieldDefinition::create('string')
->setLabel(t('Type'))
->setDescription(t('The redirect type.'));
$fields['uid'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('User ID'))
->setDescription(t('The user ID of the node author.'))
->setDefaultValueCallback('\\Drupal\\redirect\\Entity\\Redirect::getCurrentUserId')
->setSettings([
'target_type' => 'user',
]);
$fields['redirect_source'] = BaseFieldDefinition::create('redirect_source')
->setLabel(t('From'))
->setDescription(t("Enter an internal Drupal path or path alias to redirect (e.g. %example1 or %example2). Fragment anchors (e.g. %anchor) are <strong>not</strong> allowed.", [
'%example1' => 'node/123',
'%example2' => 'taxonomy/term/123',
'%anchor' => '#anchor',
]))
->setRequired(TRUE)
->setTranslatable(FALSE)
->setDisplayOptions('form', [
'type' => 'redirect_link',
'weight' => -5,
])
->setDisplayConfigurable('form', TRUE);
$fields['redirect_redirect'] = BaseFieldDefinition::create('link')
->setLabel(t('To'))
->setRequired(TRUE)
->setTranslatable(FALSE)
->setSettings([
'link_type' => LinkItemInterface::LINK_GENERIC,
'title' => DRUPAL_DISABLED,
])
->setDisplayOptions('form', [
'type' => 'link',
'weight' => -4,
])
->setDisplayConfigurable('form', TRUE);
$fields['language'] = BaseFieldDefinition::create('language')
->setLabel(t('Language'))
->setDescription(t('The redirect language.'))
->setDisplayOptions('form', [
'type' => 'language_select',
'weight' => 2,
]);
$fields['status_code'] = BaseFieldDefinition::create('integer')
->setLabel(t('Status code'))
->setDescription(t('The redirect status code.'))
->setDefaultValue(0);
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Created'))
->setDescription(t('The date when the redirect was created.'));
return $fields;
}
public static function getCurrentUserId() {
return [
\Drupal::currentUser()
->id(),
];
}
}