reinstaled drupal core using composer-drupal/drupal-project

This commit is contained in:
2019-04-29 17:50:46 +02:00
parent 1e009f88bf
commit e1f9bb3cd0
5468 changed files with 2488 additions and 395192 deletions
@@ -0,0 +1,5 @@
(function($,Drupal,drupalSettings){var settings=drupalSettings.edlp_search;var _$body=$('body');var _$container;var _$form;function init(){initEvents();initAjax();};function initEvents(){$('body').on('new-content-ajax-loaded',initAjax).on('edlp_search_search_form-col-closed',onSearchClosed);};function initAjax(){_$form=$('#edlp-search-form:not(.ajax-enabled)');if(!_$form.length)return false;void 0;_$form=$('#edlp-search-form:not(.ajax-enabled)').on('submit',onSubmitForm).addClass('ajax-enabled');_$container=_$form.parents('.row');if(!_$container.length){_$container=_$form.parent();}};function onSubmitForm(e){e.preventDefault();var args={};args.keys=$('input[type="search"]',this).val();args.entries=[];$('input[type="checkbox"]:checked','#edit-entries').each(function(index,el){args.entries.push($(this).val());});args.langues=$('input[name="language"]',this).val();args.genres=$('input[name="genres"]',this).val();void 0;if(args.keys.length>2||args.entries.length>0||args.langues.length>2||args.genres.length>2){loadResults(args);}
return false;};function loadResults(args){void 0;_$form.addClass('ajax-loading');_$body.addClass('ajax-loading');$('[theme="edlp_search_results"]',_$container).addClass('ajax-loading');var path=window.location.origin+drupalSettings.path.baseUrl+settings.results_ajax_url;$.getJSON(path,args).done(function(data){onResultsLoaded(data);}).fail(function(jqxhr,textStatus,error){onResultsLoadFail(jqxhr,textStatus,error);});};function onResultsLoaded(data){void 0;_$form.removeClass('ajax-loading');_$body.removeClass('ajax-loading');$prev_results=$('[theme="edlp_search_results"]',_$container);if($prev_results.length){$prev_results.replaceWith(data.rendered);}else{_$container.append(data.rendered);}
_$body.trigger({'type':'search-results-loaded','results':data.results_nids});if(typeof _paq!=='undefined'){var search_name=data.keys+';'+data.langues+';'+data.genres+';'+data.entry_names.join(',');_paq.push(['trackSiteSearch',search_name,'search',data.results_nids.length]);}};function onResultsLoadFail(jqxhr,textStatus,error){void 0;};function onSearchClosed(e){$('div[theme="edlp_search_results"]').remove();_$body.trigger({'type':'search-closed'});}
init();})(jQuery,Drupal,drupalSettings);
@@ -0,0 +1,130 @@
(function ($, Drupal, drupalSettings) {
var settings = drupalSettings.edlp_search;
var _$body = $('body');
var _$container;
var _$form;
function init(){
// console.log('EdlpSearch Init', settings);
initEvents();
initAjax();
};
function initEvents(){
$('body')
.on('new-content-ajax-loaded', initAjax)
.on('edlp_search_search_form-col-closed', onSearchClosed);
};
function initAjax(){
_$form = $('#edlp-search-form:not(.ajax-enabled)');
if(!_$form.length) return false;
console.log('EdlpSearch initAjaxForm()');
_$form = $('#edlp-search-form:not(.ajax-enabled)')
.on('submit', onSubmitForm)
.addClass('ajax-enabled');
_$container = _$form.parents('.row');
if(!_$container.length){
_$container = _$form.parent();
}
};
function onSubmitForm(e){
e.preventDefault();
// console.log('onSubmitForm',e);
var args = {};
// search for key words
args.keys = $('input[type="search"]', this).val();
// entries filter
args.entries = [];
$('input[type="checkbox"]:checked', '#edit-entries').each(function(index, el) {
args.entries.push($(this).val());
});
// langues
args.langues = $('input[name="language"]', this).val();
// genres
args.genres = $('input[name="genres"]', this).val();
console.log('EdlpSearch onSubmitForm() : args',args);
if(args.keys.length > 2
|| args.entries.length > 0
|| args.langues.length > 2
|| args.genres.length > 2){
loadResults(args);
}
return false;
};
function loadResults(args){
console.log('EdlpSearch loadResults() : args', args);
_$form.addClass('ajax-loading');
_$body.addClass('ajax-loading');
$('[theme="edlp_search_results"]', _$container).addClass('ajax-loading');
var path = window.location.origin + drupalSettings.path.baseUrl +settings.results_ajax_url;
$.getJSON(path, args)
.done(function(data){
onResultsLoaded(data);
})
.fail(function(jqxhr, textStatus, error){
onResultsLoadFail(jqxhr, textStatus, error);
});
};
function onResultsLoaded(data){
console.log('EdlpSearch onResultsLoaded()', data);
_$form.removeClass('ajax-loading');
_$body.removeClass('ajax-loading');
// insert results col
$prev_results = $('[theme="edlp_search_results"]', _$container);
if($prev_results.length){
$prev_results.replaceWith(data.rendered);
}else{
_$container.append(data.rendered);
}
// trigger event
_$body.trigger({
'type':'search-results-loaded',
'results':data.results_nids
});
// piwik
if(typeof _paq !== 'undefined'){
// page tracking
// https://matomo.org/blog/2017/02/how-to-track-single-page-websites-using-piwik-analytics/
// _paq.push(['setCustomUrl', state.url]);
// _paq.push(['setDocumentTitle', data.title]);
// _paq.push(['trackPageView']);
// trackEvent(category, action, [name], [value])
var search_name = data.keys+';'+data.langues+';'+data.genres+';'+data.entry_names.join(',');
// _paq.push(['trackEvent', 'AjaxSearch', 'Results', search_name, data.results_nids.length]);
// piwik track searches
// trackSiteSearch(keyword, [category], [resultsCount])
_paq.push(['trackSiteSearch', search_name, 'search', data.results_nids.length]);
}
};
function onResultsLoadFail(jqxhr, textStatus, error){
console.warn('EdlpSearch : search results ajax load failed : '+error, jqxhr.responseText);
};
function onSearchClosed(e){
// console.log('Edlp Search onSearchClosed()');
$('div[theme="edlp_search_results"]').remove();
// trigger event
_$body.trigger({'type':'search-closed'});
}
init();
})(jQuery, Drupal, drupalSettings);
@@ -0,0 +1,7 @@
name: 'edlp_search'
type: module
description: 'Edlp search module'
core: 8.x
package: Edlp
dependencies:
- search_api
@@ -0,0 +1,7 @@
edlp_search-library:
js:
assets/dist/scripts/edlp_search.min.js: { scope: footer }
dependencies:
- core/drupalSettings
- core/jquery
- core/drupal.autocomplete
@@ -0,0 +1,58 @@
<?php
/**
* @file
* Contains edlp_search.module.
*/
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
/**
* Implements hook_help().
*/
function edlp_search_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the edlp_search module.
case 'help.page.edlp_search':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Edlp search module') . '</p>';
return $output;
default:
}
}
/**
* Implements hook_page_attachments().
* @param array $attachments
*/
function edlp_search_page_attachments(array &$attachments) {
$attachments['#attached']['library'][] = 'edlp_search/edlp_search-library';
$url = Url::fromRoute('edlp_search.edlp_search_controller_search_results_ajax');
$attachments['#attached']['drupalSettings']['edlp_search']['results_ajax_url'] = $url->getInternalPath();
}
/**
* Implements hook_theme().
*/
function edlp_search_theme($existing, $type, $theme, $path) {
return array(
'edlp_search_search_form' => array(
'file' => 'includes/edlp_search_search_form.inc',
'variables' => array(
'form' => null,
),
),
'edlp_search_results' => array(
'file' => 'includes/edlp_search_results.inc',
'variables' => array(
'items' => null,
'no_results_found' => '',
'search_help' => ''
),
),
);
}
@@ -0,0 +1,49 @@
edlp_search.edlp_search_controller_searchForm:
path: '/search'
defaults:
_controller: '\Drupal\edlp_search\Controller\EdlpSearchController::searchForm'
_title: 'Search Form'
requirements:
_permission: 'access content'
edlp_search.edlp_search_controller_searchForm_ajax:
path: '/search/ajax'
defaults:
_controller: '\Drupal\edlp_search\Controller\EdlpSearchController::searchFormJson'
_title: 'Search Form'
requirements:
_permission: 'access content'
edlp_search.edlp_search_controller_autocomplete:
path: '/edlp_search/searchform/autocomplete'
defaults:
_controller: '\Drupal\edlp_search\Controller\EdlpSearchController::autocomplete'
_title: 'Autocomplete'
requirements:
_permission: 'access content'
edlp_search.edlp_search_controller_search_results:
path: '/edlp_search/searchresults'
defaults:
_controller: '\Drupal\edlp_search\Controller\EdlpSearchController::searchResults'
_title: 'Search Results'
requirements:
_permission: 'access content'
edlp_search.edlp_search_controller_search_results_ajax:
path: '/edlp_search/searchresults/ajax'
defaults:
_controller: '\Drupal\edlp_search\Controller\EdlpSearchController::searchResultsJson'
_title: 'Search Results'
requirements:
_permission: 'access content'
edlp_search.edlp_search_form:
path: '/edlp_search/form/search'
defaults:
_form: '\Drupal\edlp_search\Form\EdlpSearchForm'
_title: 'EdlpSearchForm'
requirements:
_access: 'TRUE'
@@ -0,0 +1,17 @@
<?php
function template_preprocess_edlp_search_results(&$vars){
// dpm($vars);
$view_builder = \Drupal::entityTypeManager()->getViewBuilder('node');
$items = [];
if(count($vars['items'])){
foreach ($vars['items'] as $item) {
$items[] = $view_builder->view($item, 'search_index');
}
}
$vars['items'] = $items;
}
@@ -0,0 +1,10 @@
<?php
function template_preprocess_edlp_search_search_form(&$vars){
// dpm($vars);
/*
@see https://www.drupal8.ovh/index.php/en/tutoriels/339/render-a-node-or-an-entity
*/
// $view_builder = \Drupal::entityTypeManager()->getViewBuilder($vars['entity_type']);
// $vars['content'] = $view_builder->view($vars['entity'], $vars['view_mode']);
}
@@ -0,0 +1,393 @@
<?php
namespace Drupal\edlp_search\Controller;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Form\FormBuilder;
use Drupal\search_api\Entity\Index;
use Drupal\Core\Url;
use Drupal\Core\Language\LanguageInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
// use Drupal\Core\Cache\CacheableJsonResponse;
// use Drupal\Core\Cache\CacheableMetadata;
use Drupal\core\render\RenderContext;
/**
* Class EdlpSearchController.
*/
class EdlpSearchController extends ControllerBase {
/**
* The form builder.
*
* @var \Drupal\Core\Form\FormBuilder
*/
protected $formBuilder;
protected $sapi_index_id;
/**
* Construct.
*/
public function __construct(FormBuilder $formBuilder) {
$this->formBuilder = $formBuilder;
$this->sapi_index_id = "collection";
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('form_builder')
);
}
private function getSearchForm(){
$this->form = $this->formBuilder->getForm('Drupal\edlp_search\Form\EdlpSearchForm');
}
private function buildRenderable(){
$this->getSearchForm();
$this->renderable = array(
"#theme" => "edlp_search_search_form",
'#form' => $this->form
);
}
/**
* Searchform.
*
* @return string
* Return Hello string.
*/
public function searchForm() {
$this->buildRenderable();
return $this->renderable;
}
/**
* searchFormJson
*/
public function searchFormJson(){
$this->buildRenderable();
$rendered = render($this->renderable);
$data = [
'rendered' => $rendered,
'title' => 'Search',
];
// translations links
$route_name = 'edlp_search.edlp_search_controller_searchForm';
$links = \Drupal::languageManager()->getLanguageSwitchLinks(LanguageInterface::TYPE_URL, Url::fromRoute($route_name));
if (isset($links->links)) {
$translations_build = [
'#theme' => 'links__language_block',
'#links' => $links->links,
'#attributes' => ['class' => ["language-switcher-{$links->method_id}",],],
'#set_active_class' => TRUE,
];
$translations_rendered = \Drupal::service('renderer')->executeInRenderContext(new RenderContext(), function () use ($translations_build) {return render($translations_build);});
$data['translations_links'] = $translations_rendered;
}
// TODO: make respponse cachable
$response = new JsonResponse();
$response->setData($data);
return $response;
}
public function autocomplete(Request $request){
// TODO: get field_name parameter
$this->request = $request;
$this->getRequestAutocompleteArgs();
// run a search on selected field and get the fields unique values
$this->autocompleteQuery();
$response = new JsonResponse($this->matches);
return $response;
// return array(
// "#markup" => "autocomplete"
// );
}
/**
* autocompleteQuery
*/
private function autocompleteQuery(){
// TODO: get the sapi index id with settings
/* @var $sapi_index \Drupal\search_api\IndexInterface */
$sapi_index = Index::load($this->sapi_index_id);
// dpm($sapi_index);
// Create the query.
$query = $sapi_index->query();
$query->setSearchID('edlp_search:autocomplete');
$parse_mode = \Drupal::getContainer()
->get('plugin.manager.search_api.parse_mode')
->createInstance('direct');
$query->setParseMode($parse_mode);
$current_langcode = \Drupal::languageManager()->getCurrentLanguage()->getId();
$query->setLanguages(array($current_langcode));
// $query->range(0, 20);
// workflow
$wf_condition_group = $query->createConditionGroup('OR');
$wf_condition_group->addCondition('field_workflow', 'corpus_documents_publie', "=");
// TODO: add condition with the other workflow field
$query->addConditionGroup($wf_condition_group);
// Search for keys.
if (!empty($this->keys)) {
// dpm($this->keys);
$query->keys($this->keys);
}
if(null !== $this->field_synonyms){
// select the taxo field AND the synonym field in which search will be performed
$query->setFulltextFields(array($this->field_name, $this->field_synonyms));
}else{
// OR select the unique taxo field in which search will be performed
$query->setFulltextFields(array($this->field_name));
}
$result = $query->execute();
$items = $result->getResultItems();
// dpm($items);
$this->matches = [];
foreach ($items as $item) {
// get the field from item
try {
/** @var \Drupal\Core\Entity\EntityInterface $entity */
$fields = $item->getField($this->field_name)->getValues();
}catch (SearchApiException $e) {
// if error on getinfg the field, skip item
continue;
}
// if no fields returned, skip item
if (!$fields) {
continue;
}
// dpm($fields);
// get field content
$field_text = $fields[0]->getText();
// if content already in matches, skip item
if( in_array($field_text, $this->matches) ){
continue;
}
// add the item to the return list
$this->matches[] = $field_text;
// do not return more than 14 items
if(count($this->matches) > 14){
break;
}
}
// dpm($this->matches);
}
/**
* getRequestArgs
*/
private function getRequestAutocompleteArgs(){
// args are provided by form definition with autocomplete_route_parameters
// dpm($this->request);
$this->field_name = $this->request->query->get('field_name');
// if($this->request->query->get('field_synonyms')){
$this->field_synonyms = $this->request->query->get('field_synonyms');
// }
// if($this->request->query->get('node_type')){
// $this->node_type = $this->request->query->get('node_type');
// }
$this->keys = $this->request->query->get('q');
}
/**
* searchResults
*/
public function searchResults(Request $request) {
$this->request = $request;
$this->getRequestSearchArgs();
return $this->getrenderable();
}
/**
* searchResultsJson
*/
public function searchResultsJson(Request $request){
$this->request = $request;
$this->getRequestSearchArgs();
$renderable = $this->getrenderable();
$rendered = render($renderable);
// build an array of results's nids
$results_nids = [];
foreach ($this->items as $item) {
$results_nids[] = $item->id();
}
$response = new JsonResponse();
$response->setData([
'test'=>'search results',
'keys'=>$this->keys,
'entries' => $this->entries,
'entry_names' => $this->entry_names,
'langues' => $this->langues,
'genres' => $this->genres,
'rendered'=> $rendered,
'results_nids'=>$results_nids,
]);
return $response;
}
/**
* getRenderable
*/
private function getRenderable(){
$this->query();
$build = array(
'#theme' => 'edlp_search_results',
'#items' => $this->items
);
if(!count($this->items)){
$build['#no_results_found'] = array(
'#markup' => t('Your search yielded no results.')
);
// $build['#search_help'] = array(
// '#markup' => $this->t('<ul>
// <li>Check if your spelling is correct.</li>
// <li>Remove quotes around phrases to search for each word individually. <em>bike shed</em> will often show more results than <em>&quot;bike shed&quot;</em>.</li>
// <li>Consider loosening your query with <em>OR</em>. <em>bike OR shed</em> will often show more results than <em>bike shed</em>.</li>
// </ul>')
// );
}
return $build;
}
/**
* query
*/
private function query(){
// TODO: get the sapi index id with settings
/* @var $sapi_index \Drupal\search_api\IndexInterface */
$sapi_index = Index::load($this->sapi_index_id);
// dpm($sapi_index);
// Create the query.
$query = $sapi_index->query();
// dpm($query);
$query->setSearchID('edlp_search:ajaxsearch');
$parse_mode = \Drupal::getContainer()
->get('plugin.manager.search_api.parse_mode')
->createInstance('direct');
$query->setParseMode($parse_mode);
// workflow
$wf_condition_group = $query->createConditionGroup('OR');
$wf_condition_group->addCondition('field_workflow', 'corpus_documents_publie', "=");
// TODO: add condition with the other workflow field
$query->addConditionGroup($wf_condition_group);
// Search for keys.
if (!empty($this->keys)) {
// $query->keys($this->keys);
$keys_condition_group = $query->createConditionGroup('OR');
$keys_condition_group->addCondition('title', $this->keys, 'IN');
$keys_condition_group->addCondition('field_description', $this->keys, 'IN');
$query->addConditionGroup($keys_condition_group);
}
// langues
if (!empty($this->langues)) {
$query->addCondition('field_langues', $this->langues, "=");
}
// genres
if (!empty($this->genres)) {
$query->addCondition('field_genres', $this->genres, "=");
}
// entries
$this->entry_names = [];
if (!empty($this->entries)){
$terms = entity_load_multiple('taxonomy_term', $this->entries);
$entries_condition_group = $query->createConditionGroup();
foreach ($terms as $tid => $term) {
$entries_condition_group->addCondition('field_entrees', (int)$tid, "=");
$this->entry_names[] = $term->getName();
}
// dpm($entries_condition_group);
$query->addConditionGroup($entries_condition_group);
}
// TODO: locuteurs
$current_langcode = \Drupal::languageManager()->getCurrentLanguage()->getId();
$query->setLanguages(array($current_langcode));
// $language = \Drupal::languageManager()->getCurrentLanguage()->getId();
// // $language = \Drupal::languageManager()->getCurrentLanguage()->getName();
// $query->addCondition('search_api_language', $language);
$result = $query->execute();
$items = $result->getResultItems();
// dpm($items);
$this->items = [];
foreach ($items as $item) {
try {
/** @var \Drupal\Core\Entity\EntityInterface $entity */
$entity = $item->getOriginalObject()->getValue();
}
catch (SearchApiException $e) {
continue;
}
if (!$entity) {
continue;
}
$this->items[] = $entity;
}
// dpm($this->items);
}
/**
* getRequestArgs
*/
private function getRequestSearchArgs(){
// dpm($this->request);
$this->keys = $this->request->query->get('keys');
$this->langues = $this->request->query->get('langues');
$this->genres = $this->request->query->get('genres');
$this->entries = $this->request->query->get('entries');
}
}
@@ -0,0 +1,130 @@
<?php
namespace Drupal\edlp_search\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\workflow\Entity\WorkflowManager;
/**
* Class EdlpSearchForm.
*/
class EdlpSearchForm extends FormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'edlp_search_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form['fieldset_search'] = [
'#type' => 'fieldgroup',
'#title' => $this->t('Search'),
'keys' => [
'#type' => 'search',
// '#autocomplete_route_name' => 'edlp_search.edlp_search_controller_autocomplete',
// '#autocomplete_route_parameters' => array('field_name' => 'title'),
'#maxlength' => 20,
'#size' => 15,
'#weight' => '0',
]
];
$form['fieldset_langues'] = [
'#type' => 'fieldgroup',
'#title' => $this->t('Language'),
'language' => [
'#type' => 'textfield',
'#autocomplete_route_name' => 'edlp_search.edlp_search_controller_autocomplete',
'#autocomplete_route_parameters' => array('field_name' => 'field_langues'),
'#maxlength' => 20,
'#size' => 15,
'#weight' => '0',
]
];
$form['fieldset_genre'] = [
'#type'=>'fieldgroup',
'#title' => $this->t('Genres'),
'genres' => [
'#type' => 'textfield',
'#autocomplete_route_name' => 'edlp_search.edlp_search_controller_autocomplete',
'#autocomplete_route_parameters' => array('field_name' => 'field_genres', 'field_synonyms' => 'field_synonyms'),
'#maxlength' => 20,
'#size' => 15,
'#weight' => '0',
]
];
$this->getEntries();
$form['entries'] = [
'#type' => 'checkboxes',
'#title' => $this->t('Entries'),
'#options' => $this->entries,
'#weight' => '1',
];
$form['search'] = [
'#type' => 'submit',
'#title' => $this->t('Search'),
'#value' => $this->t('Search'),
'#weight' => '99',
];
return $form;
}
private function getEntries(){
$query = \Drupal::entityQuery('taxonomy_term')
->condition('vid', 'entrees');
$tids = $query->execute();
$terms = entity_load_multiple('taxonomy_term', $tids);
// dsm($terms);
foreach ($terms as $term) {
// dpm($term->toArray());
$tid = $term->id();
$name = $term->getName();
// remove masqué
$sid = WorkflowManager::getCurrentStateId($term, 'field_workflow');
if($sid == 'generique_masque') continue;
$entries[$tid] = $name;
}
asort($entries);
$this->entries = $entries;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
// if (strlen($form_state->getValue('keys')) < 3) {
// $form_state->setErrorByName('keys', $this->t('Search string is to short. Please enter more than 3 characters.'));
// }
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// Display result.
foreach ($form_state->getValues() as $key => $value) {
drupal_set_message($key . ': ' . $value);
}
}
}
@@ -0,0 +1,39 @@
<?php
namespace Drupal\edlp_search\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Url;
/**
* Provides a 'EdlpSearchLinkBlock' block.
*
* @Block(
* id = "edlp_search_link_block",
* admin_label = @Translation("Edlp search link block"),
* )
*/
class EdlpSearchLinkBlock extends BlockBase {
/**
* {@inheritdoc}
*/
public function build() {
$build = [];
$url = Url::fromRoute('edlp_search.edlp_search_controller_searchForm');
$build['edlp_search_link_block'] = array(
'#title' => "Search",
'#type' => 'link',
'#url' => $url,
'#options'=>array(
'attributes' => array(
'data-drupal-link-system-path' => $url->getInternalPath(),
'class' => array('ajax-link'),
'alt' => t('Search sounds by title, speaker, language, genre or entry'),
)
)
);
return $build;
}
}
@@ -0,0 +1,3 @@
{{ items }}
{{ no_results_found }}
{{ search_help }}