Skip to content
Snippets Groups Projects
Commit 3be4955c authored by Lawxen Liu's avatar Lawxen Liu
Browse files

Resolve #3460408 "Override library by visibility"

parent 834bf5d4
Branches
Tags 3.0.1-beta1
1 merge request!7Resolve #3460408 "Override library by visibility"
......@@ -195,7 +195,13 @@ function library_manager_build_libraries() {
}
if ($target = $definition->get('target')) {
$data['overrides'][$target] = $library_info;
if ($definition->get('load')) {
if ($definition->get('override_by_visibility')) {
$data['libraries'][$definition->id()] = $library_info;
} else {
$data['overrides'][$target] = $library_info;
}
}
}
else {
$data['libraries'][$definition->id()] = $library_info;
......@@ -214,8 +220,24 @@ function library_manager_page_attachments(array &$attachments) {
/** @var Drupal\Core\Config\Entity\ConfigEntityStorageInterface $storage */
$storage = \Drupal::service('entity_type.manager')->getStorage('library_definition');
$library_definition_new_query = Drupal::entityQuery('library_definition')
->condition('target', NULL, 'IS NULL')
->condition('load', TRUE)
->accessCheck(FALSE)
->execute();
$library_definitions_new = \Drupal::service('entity_type.manager')->getStorage('library_definition')->loadMultiple($library_definition_new_query);
$library_definitions_override_by_visibility_query = Drupal::entityQuery('library_definition')
->condition('target', NULL, 'IS NOT NULL')
->condition('load', TRUE)
->condition('override_by_visibility', TRUE)
->accessCheck(FALSE)
->execute();
$library_definitions_override_by_visibility = \Drupal::service('entity_type.manager')->getStorage('library_definition')->loadMultiple($library_definitions_override_by_visibility_query);
/** @var Drupal\library_manager\LibraryDefinitionInterface[] $definitions */
$definitions = $storage->loadByProperties(['load' => TRUE]);
$definitions = $library_definitions_new + $library_definitions_override_by_visibility;
$context_repository = \Drupal::service('context.repository');
$context_handler = \Drupal::service('context.handler');
......@@ -246,6 +268,50 @@ function library_manager_page_attachments(array &$attachments) {
}
}
/**
* Implements hook_page_attachments_alter().
*/
function library_manager_page_attachments_alter(array &$attachments) {
/** @var Drupal\Core\Config\Entity\ConfigEntityStorageInterface $storage */
$storage = \Drupal::service('entity_type.manager')->getStorage('library_definition');
/** @var Drupal\library_manager\LibraryDefinitionInterface[] $definitions */
$definitions = $storage->loadByProperties(['override_by_visibility' => TRUE, 'load' => TRUE]);
$context_repository = \Drupal::service('context.repository');
$context_handler = \Drupal::service('context.handler');
foreach ($definitions as $definition) {
if ($target = $definition->get('target')) {
foreach ($definition->getVisibilityConditions() as $condition) {
$attach = TRUE;
$missing_context = FALSE;
if ($condition instanceof ContextAwarePluginInterface) {
try {
$contexts = $context_repository->getRuntimeContexts(array_values($condition->getContextMapping()));
$context_handler->applyContextMapping($condition, $contexts);
}
catch (ContextException $exception) {
$missing_context = TRUE;
}
}
if ($missing_context || !$condition->execute()) {
$attach = FALSE;
break;
}
}
if ($attach) {
// Remove the library if it exists in the attachments.
if (isset($attachments['#attached']['library']) && ($key = array_search($target, $attachments['#attached']['library'])) !== FALSE) {
unset($attachments['#attached']['library'][$key]);
}
}
}
}
}
/**
* Implements hook_codemirror_editor_assets_alter().
*/
......
......@@ -15,9 +15,10 @@ class LibraryDefinitionListBuilder extends ConfigEntityListBuilder {
*/
public function buildHeader() {
$header['id'] = $this->t('Machine name');
$header['target'] = $this->t('New/Override');
$header['version'] = $this->t('Version');
$header['override_by_visibility'] = $this->t('Override by visibility');
$header['load'] = $this->t('Enabled');
$header['license'] = $this->t('License');
return $header + parent::buildHeader();
}
......@@ -27,9 +28,10 @@ class LibraryDefinitionListBuilder extends ConfigEntityListBuilder {
public function buildRow(EntityInterface $entity) {
/** @var \Drupal\library_manager\LibraryDefinitionInterface $entity */
$row['id'] = $entity->id();
$row['target'] = ($target = $entity->get('target')) ? 'Override:\''.$target.'\'' : 'New:\'library_manager/'.$entity->id().'\'';
$row['version'] = $entity->get('version');
$row['override_by_visibility'] = $entity->get('override_by_visibility') ? $this->t('Yes') : $this->t('No');
$row['load'] = $entity->get('load') ? $this->t('Yes') : $this->t('No');
$row['license'] = $entity->get('license')['name'];
return $row + parent::buildRow($entity);
}
......
......@@ -54,6 +54,7 @@ use Drupal\library_manager\LibraryDefinitionInterface;
* "css" = "css",
* "library_dependencies" = "library_dependencies",
* "load" = "load",
* "override_by_visibility" = "override_by_visibility",
* "visibility" = "visibility"
* }
* )
......
......@@ -389,13 +389,36 @@ class LibraryDefinitionForm extends EntityForm {
],
];
$form['override_by_visibility'] = [
'#type' => 'checkbox',
'#title' => $this->t('Override the library just by below visibility rules'),
'#default_value' => $this->entity->get('override_by_visibility'),
'#states' => [
'visible' => [
':input[name="mode"]' => ['value' => 'override'],
],
],
];
$form['visibility'] = $this->buildVisibilityInterface([], $form_state);
$form['load'] = [
'#type' => 'checkbox',
'#title' => $this->t('Load the library automatically according to visibility rules'),
'#title' => $this->t('Enabled'),
'#default_value' => $this->entity->get('load'),
];
$form['visibility'] = $this->buildVisibilityInterface([], $form_state);
$form['label_on_new'] = [
'#type' => 'item',
'#title' => $this->t("When register new library, Uncheck this will make the library won't automatically loaded by above visibility rules, but you can still reference this library on other modules/themes."),
'#states' => [
'visible' => [
':input[name="mode"]' => ['value' => 'new'],
],
],
];
return $form;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment