99use Drupal \Core \Link ;
1010use Drupal \Core \Url ;
1111use Drupal \patternkit \Pattern ;
12+ use Exception ;
1213use Symfony \Component \DependencyInjection \ContainerInterface ;
1314use Symfony \Component \HttpFoundation \JsonResponse ;
1415use Symfony \Component \HttpFoundation \Request ;
@@ -30,7 +31,7 @@ class PatternkitController extends ControllerBase {
3031 *
3132 * @var \Drupal\patternkit\PatternkitLibraryDiscoveryInterface
3233 */
33- protected $ patternLibraryDiscovery ;
34+ protected $ libraryDiscovery ;
3435
3536 /**
3637 * The theme handler.
@@ -44,17 +45,17 @@ class PatternkitController extends ControllerBase {
4445 *
4546 * @param \Drupal\Core\Entity\EntityStorageInterface $patternkit_storage
4647 * The Patternkit storage.
47- * @param \Drupal\Core\Asset\LibraryDiscoveryInterface $pattern_library_discovery
48+ * @param \Drupal\Core\Asset\LibraryDiscoveryInterface $library_discovery
4849 * The Pattern Library Collector.
4950 * @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler
5051 * The theme handler.
5152 */
5253 public function __construct (
5354 EntityStorageInterface $ patternkit_storage ,
54- LibraryDiscoveryInterface $ pattern_library_discovery ,
55+ LibraryDiscoveryInterface $ library_discovery ,
5556 ThemeHandlerInterface $ theme_handler ) {
5657 $ this ->patternkitStorage = $ patternkit_storage ;
57- $ this ->patternLibraryDiscovery = $ pattern_library_discovery ;
58+ $ this ->libraryDiscovery = $ library_discovery ;
5859 $ this ->themeHandler = $ theme_handler ;
5960 }
6061
@@ -69,13 +70,13 @@ public function __construct(
6970 public static function create (ContainerInterface $ container ) {
7071 /** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_manager */
7172 $ entity_manager = $ container ->get ('entity_type.manager ' );
72- /** @var \Drupal\Core\Asset\LibraryDiscoveryInterface $pattern_library_discovery */
73- $ pattern_library_discovery = $ container ->get ('patternkit.library.discovery ' );
74- /** @var ThemeHandlerInterface $theme_handler */
73+ /** @var \Drupal\Core\Asset\LibraryDiscoveryInterface $library_discovery */
74+ $ library_discovery = $ container ->get ('patternkit.library.discovery ' );
75+ /** @var \Drupal\Core\Extension\ ThemeHandlerInterface $theme_handler */
7576 $ theme_handler = $ container ->get ('theme_handler ' );
7677 return new static (
7778 $ entity_manager ->getStorage ('patternkit_block ' ),
78- $ pattern_library_discovery ,
79+ $ library_discovery ,
7980 $ theme_handler
8081 );
8182 }
@@ -88,15 +89,23 @@ public static function create(ContainerInterface $container) {
8889 * added.
8990 */
9091 public function add (): array {
91- $ types = $ this ->patternLibraryDiscovery ->getAssets ();
92+ try {
93+ $ types = $ this ->libraryDiscovery ->getAssets ();
94+ }
95+ catch (Exception $ exception ) {
96+ $ this ->getLogger ('patternkit ' )->error ('Error loading pattern library assets: ' , ['@message ' => $ exception ->getMessage ()]);
97+ return [
98+ '#markup ' => $ this ->t ('Error loading pattern library assets - check the log or the format of your libraries.yml. ' ),
99+ ];
100+ }
92101 if (count ($ types ) === 0 ) {
93102 return [
94103 '#markup ' => $ this ->t ('Could not find any patterns to load. Did you add a theme or module that includes a patterns definition in the libraries.yml? ' ),
95104 ];
96105 }
97106 $ content ['types ' ] = [];
98107 $ query = \Drupal::request ()->query ->all ();
99- /** @var Pattern $type */
108+ /** @var \Drupal\patternkit\ Pattern $type */
100109 foreach ($ types as $ type ) {
101110 $ id = $ type ->getId ();
102111 $ label = $ type ->getLabel ();
@@ -133,9 +142,9 @@ public function addForm($pattern_id, Request $request): array {
133142 ]);
134143 if (($ theme = $ request ->query ->get ('theme ' ))
135144 && array_key_exists ($ theme , $ this ->themeHandler ->listInfo ())) {
136- // We have navigated to this page from the block library and will keep track
137- // of the theme for redirecting the user to the configuration page for the
138- // newly created block in the given theme.
145+ // We have navigated to this page from the block library and will keep
146+ // track of the theme for redirecting the user to the configuration page
147+ // for the newly created block in the given theme.
139148 $ block ->setTheme ($ theme );
140149 }
141150 return $ this ->entityFormBuilder ()->getForm ($ block );
@@ -152,8 +161,14 @@ public function addForm($pattern_id, Request $request): array {
152161 * @return \Symfony\Component\HttpFoundation\JsonResponse
153162 * The schema response.
154163 */
155- public function ApiPatternSchema ($ library , $ pattern ): JsonResponse {
156- return new JsonResponse ($ this ->patternLibraryDiscovery ->getLibraryAsset ("$ library. $ pattern " ));
164+ public function apiPatternSchema ($ library , $ pattern ): JsonResponse {
165+ try {
166+ $ response = $ this ->libraryDiscovery ->getLibraryAsset ("$ library. $ pattern " );
167+ }
168+ catch (Exception $ exception ) {
169+ $ response = ['error ' => $ exception ->getMessage ()];
170+ }
171+ return new JsonResponse ($ response );
157172 }
158173
159174 /**
0 commit comments