@@ -84,6 +84,11 @@ public function displaysExposed() {
8484 protected function defineOptions () {
8585 $ options = parent ::defineOptions ();
8686
87+ // Allow to attach the view to entity types / bundles.
88+ // Similar to the EVA module.
89+ $ options ['entity_type ' ]['default ' ] = '' ;
90+ $ options ['bundles ' ]['default ' ] = [];
91+
8792 // Set the default plugins to 'graphql'.
8893 $ options ['style ' ]['contains ' ]['type ' ]['default ' ] = 'graphql ' ;
8994 $ options ['exposed_form ' ]['contains ' ]['type ' ]['default ' ] = 'graphql ' ;
@@ -206,6 +211,30 @@ public function optionsSummary(&$categories, &$options) {
206211 'title ' => $ this ->t ('Query name ' ),
207212 'value ' => views_ui_truncate ($ this ->getGraphQLQueryName (), 24 ),
208213 ];
214+
215+ if ($ entity_type = $ this ->getOption ('entity_type ' )) {
216+ $ entity_info = \Drupal::entityManager ()->getDefinition ($ entity_type );
217+ $ type_name = $ entity_info ->get ('label ' );
218+
219+ $ bundle_names = [];
220+ $ bundle_info = \Drupal::entityManager ()->getBundleInfo ($ entity_type );
221+ foreach ($ this ->getOption ('bundles ' ) as $ bundle ) {
222+ $ bundle_names [] = $ bundle_info [$ bundle ]['label ' ];
223+ }
224+ }
225+
226+ $ options ['entity_type ' ] = [
227+ 'category ' => 'graphql ' ,
228+ 'title ' => $ this ->t ('Entity type ' ),
229+ 'value ' => empty ($ type_name ) ? $ this ->t ('None ' ) : $ type_name ,
230+ ];
231+
232+ $ options ['bundles ' ] = [
233+ 'category ' => 'graphql ' ,
234+ 'title ' => $ this ->t ('Bundles ' ),
235+ 'value ' => empty ($ bundle_names ) ? $ this ->t ('All ' ) : implode (', ' , $ bundle_names ),
236+ ];
237+
209238 }
210239
211240 /**
@@ -223,6 +252,41 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) {
223252 '#default_value ' => $ this ->getGraphQLQueryName (),
224253 ];
225254 break ;
255+
256+ case 'entity_type ' :
257+ $ entity_info = \Drupal::entityManager ()->getDefinitions ();
258+ $ entity_names = [NULL => $ this ->t ('None ' )];
259+ foreach ($ entity_info as $ type => $ info ) {
260+ // is this a content/front-facing entity?
261+ if ($ info instanceof \Drupal \Core \Entity \ContentEntityType) {
262+ $ entity_names [$ type ] = $ info ->get ('label ' );
263+ }
264+ }
265+
266+ $ form ['#title ' ] .= $ this ->t ('Entity type ' );
267+ $ form ['entity_type ' ] = [
268+ '#type ' => 'radios ' ,
269+ '#required ' => FALSE ,
270+ '#title ' => $ this ->t ('Attach this display to the following entity type ' ),
271+ '#options ' => $ entity_names ,
272+ '#default_value ' => $ this ->getOption ('entity_type ' ),
273+ ];
274+ break ;
275+
276+ case 'bundles ' :
277+ $ options = [];
278+ $ entity_type = $ this ->getOption ('entity_type ' );
279+ foreach (\Drupal::entityManager ()->getBundleInfo ($ entity_type ) as $ bundle => $ info ) {
280+ $ options [$ bundle ] = $ info ['label ' ];
281+ }
282+ $ form ['#title ' ] .= $ this ->t ('Bundles ' );
283+ $ form ['bundles ' ] = [
284+ '#type ' => 'checkboxes ' ,
285+ '#title ' => $ this ->t ('Attach this display to the following bundles. If no bundles are selected, the display will be attached to all. ' ),
286+ '#options ' => $ options ,
287+ '#default_value ' => $ this ->getOption ('bundles ' ),
288+ ];
289+ break ;
226290 }
227291 }
228292
@@ -236,6 +300,27 @@ public function submitOptionsForm(&$form, FormStateInterface $form_state) {
236300 case 'graphql_query_name ' :
237301 $ this ->setOption ($ section , $ form_state ->getValue ($ section ));
238302 break ;
303+ case 'entity_type ' :
304+ $ new_entity = $ form_state ->getValue ('entity_type ' );
305+ $ old_entity = $ this ->getOption ('entity_type ' );
306+ $ this ->setOption ('entity_type ' , $ new_entity );
307+
308+ if ($ new_entity != $ old_entity ) {
309+ // Each entity has its own list of bundles and view modes. If there's
310+ // only one on the new type, we can select it automatically. Otherwise
311+ // we need to wipe the options and start over.
312+ $ new_entity_info = \Drupal::entityManager ()->getDefinition ($ new_entity );
313+ $ new_bundles_keys = \Drupal::entityManager ()->getBundleInfo ($ new_entity );
314+ $ new_bundles = array ();
315+ if (count ($ new_bundles_keys ) == 1 ) {
316+ $ new_bundles [] = $ new_bundles_keys [0 ];
317+ }
318+ $ this ->setOption ('bundles ' , $ new_bundles );
319+ }
320+ break ;
321+ case 'bundles ' :
322+ $ this ->setOption ('bundles ' , array_values (array_filter ($ form_state ->getValue ('bundles ' ))));
323+ break ;
239324 }
240325 }
241326
0 commit comments