diff --git a/config/install/content_browser.settings.yml b/config/install/content_browser.settings.yml new file mode 100644 index 0000000..053c2c0 --- /dev/null +++ b/config/install/content_browser.settings.yml @@ -0,0 +1 @@ +use_frontend_theme: false \ No newline at end of file diff --git a/config/install/views.view.content_browser.yml b/config/install/views.view.content_browser.yml index e650d46..c9d5a40 100644 --- a/config/install/views.view.content_browser.yml +++ b/config/install/views.view.content_browser.yml @@ -362,7 +362,50 @@ display: footer: { } empty: { } relationships: { } - arguments: { } + arguments: + type: + id: type + table: node_field_data + field: type + relationship: none + group_type: group + admin_label: '' + default_action: default + exception: + value: all + title_enable: false + title: All + title_enable: false + title: '' + default_argument_type: entity_browser_widget_context + default_argument_options: + context_key: target_bundles + fallback: all + multiple: or + default_argument_skip_url: false + summary_options: + base_path: '' + count: true + items_per_page: 25 + override: false + summary: + sort_order: asc + number_of_records: 0 + format: default_summary + specify_validation: false + validate: + type: none + fail: 'not found' + validate_options: { } + glossary: false + limit: 0 + case: none + path_case: none + transform_dash: false + break_phrase: true + entity_type: node + entity_field: type + plugin_id: node_type display_extenders: { } filter_groups: operator: AND diff --git a/config/schema/content_browser.schema.yml b/config/schema/content_browser.schema.yml index bdb470a..58bdead 100644 --- a/config/schema/content_browser.schema.yml +++ b/config/schema/content_browser.schema.yml @@ -8,3 +8,11 @@ views.field.content_browser_preview: exposed_view_mode: type: boolean label: 'Exposed view mode' + +content_browser.settings: + type: config_object + label: 'Content Browser global settings' + mapping: + use_frontend_theme: + type: boolean + label: 'Use frontend theme' diff --git a/content_browser.info.yml b/content_browser.info.yml index 1d5862e..01e0a33 100644 --- a/content_browser.info.yml +++ b/content_browser.info.yml @@ -2,6 +2,7 @@ name: Content Browser description: 'Provides a default Entity Browser for default Content Entity types, using Masonry.' type: module # core: 8.x +configure: content_browser.settings dependencies: - entity_browser - entity_embed diff --git a/content_browser.install b/content_browser.install index c5d07a7..d3869c3 100644 --- a/content_browser.install +++ b/content_browser.install @@ -6,6 +6,7 @@ */ use \Drupal\Core\Config\ExtensionInstallStorage; +use Drupal\views\Entity\View; /** * Implements hook_install(). @@ -116,3 +117,63 @@ function content_browser_update_8001() { } } } + +/** + * Updates the content browser view to contextually filter by allowed bundles. + */ +function content_browser_update_8002() { + /** @var \Drupal\views\Entity\View $view */ + if ($view = View::load('content_browser') && class_exists('Drupal\entity_browser\Plugin\views\argument_default\EntityBrowserWidgetContext')) { + $display = &$view->getDisplay('default'); + $display['display_options']['arguments']['type'] = [ + 'id' => 'type', + 'table' => 'node_field_data', + 'field' => 'type', + 'relationship' => 'none', + 'group_type' => 'group', + 'admin_label' => '', + 'default_action' => 'default', + 'exception' => [ + 'value' => 'all', + 'title_enable' => FALSE, + 'title' => 'All', + ], + 'title_enable' => FALSE, + 'title' => '', + 'default_argument_type' => 'entity_browser_widget_context', + 'default_argument_options' => [ + 'context_key' => 'target_bundles', + 'fallback' => 'all', + 'multiple' => 'or', + ], + 'default_argument_skip_url' => FALSE, + 'summary_options' => [ + 'base_path' => '', + 'count' => TRUE, + 'items_per_page' => 25, + 'override' => FALSE, + ], + 'summary' => [ + 'sort_order' => 'asc', + 'number_of_records' => 0, + 'format' => 'default_summary', + ], + 'specify_validation' => FALSE, + 'validate' => [ + 'type' => 'none', + 'fail' => 'not found', + ], + 'validate_options' => [], + 'glossary' => FALSE, + 'limit' => 0, + 'case' => 'none', + 'path_case' => 'none', + 'transform_dash' => FALSE, + 'break_phrase' => TRUE, + 'entity_type' => 'node', + 'entity_field' => 'type', + 'plugin_id' => 'node_type', + ]; + $view->save(); + } +} diff --git a/content_browser.module b/content_browser.module index a138613..05b6237 100644 --- a/content_browser.module +++ b/content_browser.module @@ -45,6 +45,25 @@ function content_browser_form_alter(&$form, FormStateInterface &$form_state) { // Add a class for generic styling. $form['#attributes']['class'][] = 'content-browser-form'; } + // Remove types that are contextually disallowed by the View. + else if (isset($form['#id']) && $form['#id'] == 'views-exposed-form-content-browser-entity-browser') { + /** @var \Drupal\views\ViewExecutable $view */ + $view = $form_state->get('view'); + if ($view instanceof \Drupal\views\ViewExecutable && isset($view->argument['type'])) { + /** @var \Drupal\node\Plugin\views\argument\Type $type */ + $type = $view->argument['type']; + $value = $type->getValue(); + if (!is_null($value) && !$type->isException($value)) { + $types = explode('+', $value); + $types[] = 'All'; + foreach ($form['type']['#options'] as $name => $option) { + if (!in_array($name, $types, TRUE)) { + unset($form['type']['#options'][$name]); + } + } + } + } + } } } diff --git a/content_browser.routing.yml b/content_browser.routing.yml new file mode 100644 index 0000000..b137d62 --- /dev/null +++ b/content_browser.routing.yml @@ -0,0 +1,7 @@ +content_browser.settings: + path: '/admin/config/content-browser/settings' + defaults: + _form: '\Drupal\content_browser\Form\ContentBrowserSettingsForm' + _title: 'Content Browser settings' + requirements: + _permission: 'administer entity browsers' diff --git a/src/Form/ContentBrowserSettingsForm.php b/src/Form/ContentBrowserSettingsForm.php new file mode 100644 index 0000000..28a62c1 --- /dev/null +++ b/src/Form/ContentBrowserSettingsForm.php @@ -0,0 +1,86 @@ +routeBuilder = $route_builder; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('config.factory'), + $container->get('router.builder') + ); + } + + /** + * {@inheritdoc} + */ + public function getFormId() { + return 'content_browser_settings_form'; + } + + /** + * {@inheritdoc} + */ + public function buildForm(array $form, FormStateInterface $form_state) { + $form = parent::buildForm($form, $form_state); + + $config = $this->config('content_browser.settings'); + + $form['use_frontend_theme'] = [ + '#type' => 'checkbox', + '#title' => $this->t('Use frontend theme when rendering Content Browser'), + '#default_value' => $config->get('use_frontend_theme'), + ]; + + return $form; + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + parent::submitForm($form, $form_state); + $config = $this->config('content_browser.settings'); + $config->set('use_frontend_theme', $form_state->getValue('use_frontend_theme')); + $config->save(); + $this->routeBuilder->rebuild(); + } + + /** + * {@inheritdoc} + */ + protected function getEditableConfigNames() { + return ['content_browser.settings']; + } + +} diff --git a/src/Routing/ContentBrowserRouteSubscriber.php b/src/Routing/ContentBrowserRouteSubscriber.php index e77a2ee..43bec1a 100644 --- a/src/Routing/ContentBrowserRouteSubscriber.php +++ b/src/Routing/ContentBrowserRouteSubscriber.php @@ -19,9 +19,12 @@ class ContentBrowserRouteSubscriber extends RouteSubscriberBase { * {@inheritdoc} */ public function alterRoutes(RouteCollection $collection) { - foreach (['entity_browser.browse_content', 'entity_browser.browse_content_iframe'] as $name) { - if ($route = $collection->get($name)) { - $route->setOption('_admin_route', FALSE); + $config = \Drupal::config('content_browser.settings'); + if ($config->get('use_frontend_theme') !== FALSE) { + foreach (['entity_browser.browse_content', 'entity_browser.browse_content_iframe'] as $name) { + if ($route = $collection->get($name)) { + $route->setOption('_admin_route', FALSE); + } } } }