View source
<?php
namespace Drupal\amp;
use Drupal\Core\Url;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\DependencyInjection\ServiceProviderBase;
class EntityTypeInfo extends ServiceProviderBase {
protected $entityTypeManager;
protected $cache;
public function __construct(EntityTypeManagerInterface $entity_type_manager, CacheBackendInterface $cache) {
$this->entityTypeManager = $entity_type_manager;
$this->cache = $cache;
}
public function isAmpEnabledType($node_type) {
$amp_display = $this->entityTypeManager
->getStorage('entity_view_display')
->load('node.' . $node_type . '.amp');
if ($amp_display && $amp_display
->status()) {
return TRUE;
}
return FALSE;
}
public function getAmpEnabledTypes() {
$enabled_types = [];
if ($cache = $this->cache
->get('amp_enabled_types')) {
$enabled_types = $cache->data;
}
else {
$ids = $this->entityTypeManager
->getStorage('entity_view_display')
->getQuery()
->condition('id', 'node.', 'STARTS_WITH')
->condition('mode', 'amp')
->condition('status', TRUE)
->execute();
if ($ids) {
foreach ($ids as $id) {
$parts = explode('.', $id);
$enabled_types[$parts[1]] = $parts[1];
}
}
$this->cache
->set('amp_enabled_types', $enabled_types);
}
return $enabled_types;
}
public function getFormattedAmpEnabledTypes() {
$enabled_types = !empty($this
->getAmpEnabledTypes()) ? $this
->getAmpEnabledTypes() : array();
$node_types = node_type_get_names();
$node_status_list = array();
$destination = Url::fromRoute("amp.settings")
->toString();
foreach ($node_types as $bundle => $label) {
$configure = Url::fromRoute("entity.entity_view_display.node.view_mode", [
'node_type' => $bundle,
'view_mode_name' => 'amp',
], [
'query' => [
'destination' => $destination,
],
])
->toString();
$enable_disable = Url::fromRoute("entity.entity_view_display.node.default", [
'node_type' => $bundle,
], [
'query' => [
'destination' => $destination,
],
])
->toString();
if (in_array($bundle, $enabled_types)) {
$node_status_list[] = t('@label is <em>enabled</em>: <a href=":configure">Configure AMP view mode</a> or <a href=":enable_disable">Disable AMP display</a>', array(
'@label' => $label,
':configure' => $configure,
':enable_disable' => $enable_disable,
));
}
else {
$node_status_list[] = t('@label is <em>disabled</em>: <a href=":enable_disable">Enable AMP in Custom Display Settings</a>', array(
'@label' => $label,
':enable_disable' => $enable_disable,
));
}
}
return $node_status_list;
}
}