started edlp_search module : displaying a draft of the form

This commit is contained in:
Bachir Soussi Chiadmi
2018-03-19 00:05:20 +01:00
parent 003b381d6d
commit ff495add52
439 changed files with 70494 additions and 120 deletions
@@ -0,0 +1 @@
edlp_search:
@@ -0,0 +1,7 @@
name: 'edlp_search'
type: module
description: 'Edlp search module'
core: 8.x
package: 'edlp'
dependencies:
- search_api
@@ -0,0 +1,38 @@
<?php
/**
* @file
* Contains edlp_search.module.
*/
use Drupal\Core\Routing\RouteMatchInterface;
/**
* 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_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,
),
),
);
}
@@ -0,0 +1,40 @@
edlp_search.edlp_search_controller_searchForm:
path: '/edlp_search/searchform'
defaults:
_controller: '\Drupal\edlp_search\Controller\EdlpSearchController::searchForm'
_title: 'Search Form'
requirements:
_permission: 'access content'
edlp_search.edlp_search_controller_searchForm_ajax:
path: '/edlp_search/searchform/ajax'
defaults:
_controller: '\Drupal\edlp_search\Controller\EdlpSearchController::searchFormJson'
_title: 'Search Form'
requirements:
_permission: 'access content'
edlp_search.edlp_search_controller_searchResults:
path: '/edlp_search/searchresults'
defaults:
_controller: '\Drupal\edlp_search\Controller\EdlpSearchController::searchResults'
_title: 'Search Results'
requirements:
_permission: 'access content'
edlp_search.edlp_search_controller_searchResults_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,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,99 @@
<?php
namespace Drupal\edlp_search\Controller;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Entity\EntityStorageInterface;
// use Drupal\facets\FacetManager\DefaultFacetManager;
// use Drupal\search_api_page\Entity\SearchApiPage;
use Drupal\Core\Form\FormBuilder;
use Symfony\Component\HttpFoundation\JsonResponse;
/**
* Class EdlpSearchController.
*/
class EdlpSearchController extends ControllerBase {
/**
* The form builder.
*
* @var \Drupal\Core\Form\FormBuilder
*/
protected $formBuilder;
/**
* Construct.
*/
public function __construct(FormBuilder $formBuilder) {
$this->formBuilder = $formBuilder;
}
/**
* {@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;
}
public function searchFormJson(){
$this->buildRenderable();
$rendered = render($this->renderable);
$response = new JsonResponse();
$response->setData([
'rendered'=> $rendered,
]);
return $response;
}
/**
* Searchresults.
*
* @return string
* Return Hello string.
*/
public function searchResults() {
return [
'#type' => 'markup',
'#markup' => $this->t('Implement method: searchResults')
];
}
public function searchResultsJson(){
$response = new JsonResponse();
$response->setData([
'test'=>'search results',
// 'rendered'=> $rendered,
]);
return $response;
}
}
@@ -0,0 +1,94 @@
<?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['keys'] = [
'#type' => 'search',
// '#title' => $this->t('Keys'),
'#maxlength' => 64,
'#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;
}
$this->entries = $entries;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
}
/**
* {@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,38 @@
<?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', [], ['absolute' => TRUE]);
$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'),
)
)
);
return $build;
}
}