lb_everywhere-8.x-1.x-dev/src/Plugin/SectionStorage/RegionSectionStorage.php
src/Plugin/SectionStorage/RegionSectionStorage.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 | <?php namespace Drupal\lb_everywhere\Plugin\SectionStorage; use Drupal\Core\Access\AccessResult; use Drupal\Core\Cache\RefinableCacheableDependencyInterface; use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Entity\FieldableEntityInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Plugin\Context\EntityContext; use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Session\AccountInterface; use Drupal\Core\Url; use Drupal\layout_builder\Plugin\SectionStorage\SectionStorageBase; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\Routing\RouteCollection; /** * Provides section storage for Layout Builder Everywhere regions. * * @SectionStorage( * id = "lb_everywhere", * context_definitions = { * "display" = @ContextDefinition("entity:entity_view_display"), * }, * ) */ class RegionSectionStorage extends SectionStorageBase implements ContainerFactoryPluginInterface { /** * The entity type manager. * * @var \Drupal\Core\Entity\EntityTypeManagerInterface */ protected $entityTypeManager ; /** * The current route match. * * @var \Drupal\Core\Routing\RouteMatchInterface */ protected $routeMatch ; /** * {@inheritdoc} */ public function __construct( array $configuration , $plugin_id , $plugin_definition , RouteMatchInterface $route_match , EntityTypeManagerInterface $entity_type_manager ) { parent::__construct( $configuration , $plugin_id , $plugin_definition ); $this ->routeMatch = $route_match ; $this ->entityTypeManager = $entity_type_manager ; } /** * {@inheritdoc} */ public static function create(ContainerInterface $container , array $configuration , $plugin_id , $plugin_definition ) { return new static ( $configuration , $plugin_id , $plugin_definition , $container ->get( 'current_route_match' ), $container ->get( 'entity_type.manager' ) ); } /** * {@inheritdoc} */ protected function getSectionList() { return $this ->getContextValue( 'display' ); } /** * Gets the entity storing the defaults. * * @return \Drupal\layout_builder\Entity\LayoutEntityDisplayInterface * The entity storing the defaults. */ protected function getDisplay() { return $this ->getSectionList(); } /** * {@inheritdoc} */ public function getStorageId() { return $this ->getDisplay()->getTargetBundle(); } /** * {@inheritdoc} */ public function buildRoutes(RouteCollection $collection ) { $entity_route = $collection ->get( 'lbeverywhere.controller' ); $path = $entity_route ->getPath() . '/{storage_id}' ; $defaults = []; $requirements = []; $options = $entity_route ->getOptions(); $options [ '_admin_route' ] = FALSE; $this ->buildLayoutRoutes( $collection , $this ->getPluginDefinition(), $path , $defaults , $requirements , $options , '' , 'lbeverywhere' ); } /** * Returns an array of relevant entity types. * * @return \Drupal\Core\Entity\EntityTypeInterface[] * An array of entity types. */ protected function getEntityTypes() { return array_filter ( $this ->entityTypeManager->getDefinitions(), function (EntityTypeInterface $entity_type ) { return $entity_type ->entityClassImplements(FieldableEntityInterface:: class ) && $entity_type ->hasHandlerClass( 'form' , 'layout_builder' ) && $entity_type ->hasViewBuilderClass(); }); } /** * {@inheritdoc} */ public function getRedirectUrl() { return $this ->getLayoutBuilderUrl(); } /** * {@inheritdoc} */ public function getLayoutBuilderUrl( $rel = 'view' ) { // Viewing the region is always done on the current page. if ( $rel === 'view' ) { return Url::fromRouteMatch( $this ->routeMatch); } return Url::fromRoute( "layout_builder.{$this->getStorageType()}.$rel" , [ 'storage_id' => $this ->getStorageId()]); } /** * {@inheritdoc} */ public function deriveContextsFromRoute( $value , $definition , $name , array $defaults ) { $contexts = []; if ( $entity = $this ->extractEntityFromRoute( $value , $defaults )) { $contexts [ 'display' ] = EntityContext::fromEntity( $entity ); } return $contexts ; } /** * Extracts an entity from the route values. * * @param mixed $value * The raw value from the route. * @param array $defaults * The route defaults array. * * @return \Drupal\Core\Entity\EntityInterface|null * The entity for the route, or NULL if none exist. * * @see \Drupal\layout_builder\SectionStorageInterface::deriveContextsFromRoute() * @see \Drupal\Core\ParamConverter\ParamConverterInterface::convert() */ private function extractEntityFromRoute( $value , array $defaults ) { if ( $value ) { $storage_id = $value ; } elseif (! empty ( $defaults [ 'storage_id' ])) { $storage_id = $defaults [ 'storage_id' ]; } else { return NULL; } $storage = $this ->entityTypeManager->getStorage( 'entity_view_display' ); // If the display does not exist, create a new one. if (! $display = $storage ->load( "lbeverywhere.$storage_id.default" )) { $display = $storage ->create([ 'targetEntityType' => 'lbeverywhere' , 'bundle' => $storage_id , 'mode' => 'default' , 'status' => TRUE, ]); } return $display ; } /** * {@inheritdoc} */ public function label() { return $this ->getDisplay()->label(); } /** * {@inheritdoc} */ public function save() { return $this ->getDisplay()->save(); } /** * {@inheritdoc} */ public function isApplicable(RefinableCacheableDependencyInterface $cacheability ) { $cacheability ->addCacheableDependency( $this ); return $this ->getDisplay()->getTargetEntityTypeId() === 'lbeverywhere' ; } /** * {@inheritdoc} */ public function access( $operation , AccountInterface $account = NULL, $return_as_object = FALSE) { $result = AccessResult::allowedIf( $this ->getDisplay()->isLayoutBuilderEnabled())->addCacheableDependency( $this ); return $return_as_object ? $result : $result ->isAllowed(); } /** * {@inheritdoc} */ public function getSectionListFromId( $id ) { @trigger_error( '\Drupal\layout_builder\SectionStorageInterface::getSectionListFromId() is deprecated in drupal:8.7.0. It will be removed before drupal:9.0.0. The section list should be derived from context. See https://www.drupal.org/node/3016262' , E_USER_DEPRECATED); } /** * {@inheritdoc} */ public function extractIdFromRoute( $value , $definition , $name , array $defaults ) { @trigger_error( '\Drupal\layout_builder\SectionStorageInterface::extractIdFromRoute() is deprecated in drupal:8.7.0. It will be removed before drupal:9.0.0. \Drupal\layout_builder\SectionStorageInterface::deriveContextsFromRoute() should be used instead. See https://www.drupal.org/node/3016262' , E_USER_DEPRECATED); } } |