View source
<?php
namespace Drupal\menu_item_extras\Service;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityDefinitionUpdateManagerInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Field\FieldStorageDefinitionListenerInterface;
use Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface;
use Drupal\Core\Database\Connection;
use Drupal\Core\Form\FormStateInterface;
use Drupal\menu_link_content\MenuLinkContentInterface;
class MenuLinkContentService implements MenuLinkContentServiceInterface {
use DependencySerializationTrait;
private $entityTypeManager;
private $entityDefinitionUpdateManager;
private $entityFieldManager;
private $fieldStorageDefinitionListener;
private $entityLastInstalledSchemaRepository;
protected $connection;
public function __construct(EntityTypeManagerInterface $entityTypeManager, EntityDefinitionUpdateManagerInterface $entityDefinitionUpdateManager, EntityFieldManagerInterface $entityFieldManager, FieldStorageDefinitionListenerInterface $fieldStorageDefinitionListener, EntityLastInstalledSchemaRepositoryInterface $entityLastInstalledSchemaRepository, Connection $connection) {
$this->entityTypeManager = $entityTypeManager;
$this->entityDefinitionUpdateManager = $entityDefinitionUpdateManager;
$this->entityFieldManager = $entityFieldManager;
$this->fieldStorageDefinitionListener = $fieldStorageDefinitionListener;
$this->entityLastInstalledSchemaRepository = $entityLastInstalledSchemaRepository;
$this->connection = $connection;
}
public function installViewModeField() {
$definition = $this->entityFieldManager
->getFieldStorageDefinitions('menu_link_content')['view_mode'];
$this->fieldStorageDefinitionListener
->onFieldStorageDefinitionCreate($definition);
}
public function updateMenuItemsBundle($menu_id, $extras_enabled = TRUE) {
$menu_links = $this->entityTypeManager
->getStorage('menu_link_content')
->loadByProperties([
'menu_name' => $menu_id,
]);
if (!empty($menu_links)) {
foreach ($menu_links as $menu_link) {
$this
->updateMenuItemBundle($menu_link, $extras_enabled);
if ($menu_link
->requiresRediscovery()) {
$menu_link
->setRequiresRediscovery(FALSE);
}
$menu_link
->save();
}
}
}
public function updateMenuItemBundle(MenuLinkContentInterface $item, $extras_enabled = TRUE, $save = FALSE) {
$item
->set('bundle', $extras_enabled ? $item
->getMenuName() : $item
->getEntityTypeId());
if ($save) {
$item
->save();
}
}
public function clearMenuData($menu_id = 'all') {
$query = $this->connection
->update('menu_link_content_data')
->fields([
'view_mode' => NULL,
]);
if ($menu_id !== 'all') {
$query
->condition('menu_name', $menu_id);
}
$query
->execute();
}
public function updateMenuLinkContentBundle() {
$tables = [
"menu_link_content",
"menu_link_content_data",
];
$existing_data = [];
foreach ($tables as $table) {
$existing_data[$table] = $this->connection
->select($table)
->fields($table)
->orderBy('id', 'ASC')
->execute()
->fetchAll(\PDO::FETCH_ASSOC);
$this->connection
->truncate($table)
->execute();
}
$this->entityTypeManager
->clearCachedDefinitions();
$storage_definitions = $this->entityFieldManager
->getFieldStorageDefinitions('menu_link_content');
$original_storage_definitions = $this->entityLastInstalledSchemaRepository
->getLastInstalledFieldStorageDefinitions('menu_link_content');
$storage_definition = isset($storage_definitions['bundle']) ? $storage_definitions['bundle'] : NULL;
$original_storage_definition = isset($original_storage_definitions['bundle']) ? $original_storage_definitions['bundle'] : NULL;
$this->fieldStorageDefinitionListener
->onFieldStorageDefinitionUpdate($storage_definition, $original_storage_definition);
foreach ($tables as $table) {
if (!empty($existing_data[$table])) {
$insert_query = $this->connection
->insert($table)
->fields(array_keys(end($existing_data[$table])));
foreach ($existing_data[$table] as $row) {
$insert_query
->values(array_values($row));
}
$insert_query
->execute();
}
}
}
public function cleanupFields(ContentEntityInterface $entity) {
foreach ($entity
->getFieldDefinitions() as $field_name => $field_def) {
if (!$field_def instanceof BaseFieldDefinition) {
$entity
->set($field_name, NULL);
}
}
}
public function doEntityUpdate() {
$entity_type = $this->entityTypeManager
->getDefinition('menu_link_content');
$this->entityDefinitionUpdateManager
->updateEntityType($entity_type);
}
public function doBundleFieldUpdate() {
$entity_type = $this->entityTypeManager
->getDefinition('menu_link_content');
$this->entityDefinitionUpdateManager
->updateFieldStorageDefinition($this->entityDefinitionUpdateManager
->getFieldStorageDefinition('bundle', $entity_type
->id()));
}
public function nodeSubmit(array $form, FormStateInterface $form_state) {
$form_object = $form_state
->getFormObject();
$node = $form_object
->getEntity();
if (!$form_state
->isValueEmpty('menu')) {
$values = $form_state
->getValue('menu');
if (!empty($values['enabled']) && trim($values['title'])) {
if (!empty($values['menu_parent'])) {
list($menu_name, $parent) = explode(':', $values['menu_parent'], 2);
$values['menu_name'] = $menu_name;
$values['parent'] = $parent;
}
$link = $this
->nodeSave($node, $values);
$values['entity_id'] = $link
->id();
$form_state
->setValue('menu', $values);
}
}
}
public function nodeSave(EntityInterface $node, array $values) {
$menu_link_content_storage = $this->entityTypeManager
->getStorage('menu_link_content');
if (!empty($values['entity_id'])) {
$entity = $menu_link_content_storage
->load($values['entity_id']);
if ($entity
->isTranslatable()) {
if (!$entity
->hasTranslation($node
->language()
->getId())) {
$entity = $entity
->addTranslation($node
->language()
->getId(), $entity
->toArray());
}
else {
$entity = $entity
->getTranslation($node
->language()
->getId());
}
}
}
else {
$entity = $menu_link_content_storage
->create([
'link' => [
'uri' => 'entity:node/' . $node
->id(),
],
'langcode' => $node
->language()
->getId(),
]);
$entity
->set('enabled', 1);
}
$entity
->set('title', trim($values['title']));
$entity
->set('description', trim($values['description']));
$entity
->set('menu_name', $values['menu_name']);
$entity
->set('bundle', $values['menu_name']);
$entity
->set('parent', $values['parent']);
$entity
->set('weight', isset($values['weight']) ? $values['weight'] : 0);
$entity
->save();
return $entity;
}
}