Views https://blog.werk21.de/de de Programmatically render an exposed filter form https://blog.werk21.de/en/2017/03/08/programmatically-render-exposed-filter-form <span>Hervorgehobenes Formular einer Ansicht programmatisch ausgeben</span> <span><span lang="" about="/de/user/8" typeof="schema:Person" property="schema:name" datatype="">patrick</span></span> <span>Mi., 08.03.2017 - 09:29 Uhr</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><p>Falls Sie ein hervorgehobenes Formular einer Ansicht programmatisch ausgeben wollen, können Sie folgenden Schnipsel verwenden. Das Ergebnis ist ein <em>renderable array</em> für das hervorgehobene Formular.</p> <div class="geshifilter"><pre class="php geshifilter-php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">use</span> Drupal\Core\Form\FormState<span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">use</span> Drupal\views\Views<span style="color: #339933;">;</span>   <span style="color: #000088;">$form</span> <span style="color: #339933;">=</span> <span style="color: #009900;">[</span><span style="color: #009900;">]</span><span style="color: #339933;">;</span> <span style="color: #000088;">$view_id</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'your_view_id'</span><span style="color: #339933;">;</span> <span style="color: #000088;">$display_id</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'your_display_id'</span><span style="color: #339933;">;</span> <span style="color: #000088;">$view</span> <span style="color: #339933;">=</span> Views<span style="color: #339933;">::</span><span style="color: #004000;">getView</span><span style="color: #009900;">(</span><span style="color: #000088;">$view_id</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">(</span><span style="color: #000088;">$view</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span> <span style="color: #000088;">$view</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setDisplay</span><span style="color: #009900;">(</span><span style="color: #000088;">$display_id</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> <span style="color: #000088;">$view</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">initHandlers</span><span style="color: #009900;">(</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> <span style="color: #000088;">$form_state</span> <span style="color: #339933;">=</span> <span style="color: #009900;">(</span><span style="color: #000000; font-weight: bold;">new</span> FormState<span style="color: #009900;">(</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span> <span style="color: #339933;">-&gt;</span><span style="color: #004000;">setStorage</span><span style="color: #009900;">(</span><span style="color: #009900;">[</span> <span style="color: #0000ff;">'view'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$view</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'display'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #339933;">&amp;</span><span style="color: #000088;">$view</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">display_handler</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">display</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'rerender'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #339933;">,</span> <span style="color: #009900;">]</span><span style="color: #009900;">)</span> <span style="color: #339933;">-&gt;</span><span style="color: #004000;">setMethod</span><span style="color: #009900;">(</span><span style="color: #0000ff;">'get'</span><span style="color: #009900;">)</span> <span style="color: #339933;">-&gt;</span><span style="color: #004000;">setAlwaysProcess</span><span style="color: #009900;">(</span><span style="color: #009900;">)</span> <span style="color: #339933;">-&gt;</span><span style="color: #004000;">disableRedirect</span><span style="color: #009900;">(</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> <span style="color: #000088;">$form_state</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set</span><span style="color: #009900;">(</span><span style="color: #0000ff;">'rerender'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> <span style="color: #000088;">$form</span> <span style="color: #339933;">=</span> \Drupal<span style="color: #339933;">::</span><span style="color: #004000;">formBuilder</span><span style="color: #009900;">(</span><span style="color: #009900;">)</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">buildForm</span><span style="color: #009900;">(</span><span style="color: #0000ff;">'\Drupal\views\Form\ViewsExposedForm'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$form_state</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> <span style="color: #009900;">}</span></pre></div> <p>Als erstes lassen wir uns das <a href="https://api.drupal.org/api/drupal/core%21modules%21views%21src%21ViewExecutable.php/class/ViewExecutable/8.2.x">ViewExecutable</a>-Objekt der Ansicht von <a href="https://api.drupal.org/api/drupal/core%21modules%21views%21src%21Views.php/class/Views/8.2.x">Views</a> geben, indem wir dessen Methode <a href="https://api.drupal.org/api/drupal/core%21modules%21views%21src%21Views.php/function/Views%3A%3AgetView/8.2.x">getView</a> mit der Id der Ansicht als Parameter aufrufen. Wir setzen das korrekte Display und initialisieren die Handler. Dann erstellen wir ein neues <a href="https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Form%21FormState.php/class/FormState/8.2.x">FormState</a>-Objekt, das wir mit <a href="https://api.drupal.org/api/drupal/core%21modules%21views%21src%21Form%21ViewsExposedForm.php/class/ViewsExposedForm/8.2.x">ViewsExposedForm</a> verwenden können. Schließlich rufen wir den <a href="https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Form%21FormBuilderInterface.php/interface/FormBuilderInterface/8.2.x">FormBuilder</a>-Service auf und bauen das <em>renderable array</em> für das Formular.</p> <p>Beachten Sie: Falls das Display Ihrer Ansicht keine URL hat (z.B. weil es ein Master- oder Block-Display ist), dann wird die URL der aktuellen Seite als Action-URL des Formulars verwendet. Damit das Formular funktioniert, muss die URL eine URL sein, auf der das zugehörige Display der Ansicht angezeigt wird. Wenn Sie das Formular auf einer anderen Seite ausgeben möchten als das Display der Ansicht, müssen Sie daher die URL manuell anpassen. Das können Sie erreichen, indem Sie <span class="geshifilter"><code class="php geshifilter-php"><span style="color: #000088;">$form</span><span style="color: #009900;">[</span><span style="color: #0000ff;">'#action'</span><span style="color: #009900;">]</span></code></span> in dem <em>renderable array</em> überschreiben.</p> </div> <div class="field field--name-field-systems field--type-entity-reference field--label-inline"> <div class="field--label">Systeme</div> <a href="/de/systems/drupal-8" class="label label-default" hreflang="de">Drupal 8</a> </div> <div class="field field--name-field-tags field--type-entity-reference field--label-inline"> <div class="field--label">Tags</div> <a href="/de/tags/views" class="label label-default" hreflang="de">Views</a> <a href="/en/tags/viewsexecutable" class="label label-default" hreflang="en">ViewsExecutable</a> <a href="/en/tags/getview" class="label label-default" hreflang="en">getView</a> <a href="/en/tags/formstate" class="label label-default" hreflang="en">FormState</a> <a href="/en/tags/viewsexposedform" class="label label-default" hreflang="en">ViewsExposedForm</a> <a href="/en/tags/formbuilder" class="label label-default" hreflang="en">FormBuilder</a> <a href="/de/tags/view" class="label label-default" hreflang="de">view</a> <a href="/de/tags/exposed-form" class="label label-default" hreflang="de">exposed form</a> </div> Wed, 08 Mar 2017 08:13:34 +0000 patrick 114 at https://blog.werk21.de Get current view object from route object https://blog.werk21.de/en/2017/01/29/get-current-view-object-route-object <span>Das aktuelle View-Objekt an Hand eines Route-Objekts abrufen</span> <span><span lang="" about="/de/user/8" typeof="schema:Person" property="schema:name" datatype="">patrick</span></span> <span>So., 29.01.2017 - 14:00 Uhr</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><p>Mitunter möchtest du ein View-Objekt an Hand eines Route-Objekts abrufen. In dem folgenden Beispiel implementieren wir eine <em>preprocess-Funktion</em> für das <em>page template</em> um das <i>View-Objekt</i>, das mit dem <em>aktuellen Route-Objekt</em> assoziiert ist, abzurufen und dann den Titel des <em>aktuellen Displays</em> dieses View-Objekts zu erhalten. Diesen Titel stellen wir dann als <em>Template-Variable</em> unserem Template zur Verfügung.</p> <div class="geshifilter"><pre class="php geshifilter-php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">use</span> Drupal\views\Views<span style="color: #339933;">;</span>   <span style="color: #009933; font-style: italic;">/**  * Implements hook_preprocess_HOOK() for page.html.twig.  */</span> <span style="color: #000000; font-weight: bold;">function</span> example_preprocess_page<span style="color: #009900;">(</span><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a> <span style="color: #339933;">&amp;</span><span style="color: #000088;">$variables</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>   <span style="color: #000088;">$variables</span><span style="color: #009900;">[</span><span style="color: #0000ff;">'view_title'</span><span style="color: #009900;">]</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>   <span style="color: #666666; font-style: italic;">// Get current route object.</span>   <span style="color: #000088;">$route</span> <span style="color: #339933;">=</span> \Drupal<span style="color: #339933;">::</span><span style="color: #004000;">routeMatch</span><span style="color: #009900;">(</span><span style="color: #009900;">)</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getRouteObject</span><span style="color: #009900;">(</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>   <span style="color: #b1b100;">if</span> <span style="color: #009900;">(</span><span style="color: #000088;">$route</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span> <span style="color: #666666; font-style: italic;">// Get view id and display id from route.</span>     <span style="color: #000088;">$view_id</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$route</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getDefault</span><span style="color: #009900;">(</span><span style="color: #0000ff;">'view_id'</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>     <span style="color: #000088;">$display_id</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$route</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getDefault</span><span style="color: #009900;">(</span><span style="color: #0000ff;">'display_id'</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// If you just want the page title, you could get it directly from the </span> <span style="color: #666666; font-style: italic;">// route object. Unfortunately, it will be untranslated, so if we want</span> <span style="color: #666666; font-style: italic;">// to get the translated title, we still need to load the view object.</span>     <span style="color: #666666; font-style: italic;">// $route-&gt;getDefault('_title');</span>     <span style="color: #b1b100;">if</span> <span style="color: #009900;">(</span><span style="color: #339933;">!</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">(</span><span style="color: #000088;">$view_id</span><span style="color: #009900;">)</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">(</span><span style="color: #000088;">$display_id</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span> <span style="color: #666666; font-style: italic;">// Get the view by id.</span>       <span style="color: #000088;">$view</span> <span style="color: #339933;">=</span> Views<span style="color: #339933;">::</span><span style="color: #004000;">getView</span><span style="color: #009900;">(</span><span style="color: #000088;">$view_id</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>       <span style="color: #b1b100;">if</span> <span style="color: #009900;">(</span><span style="color: #000088;">$view</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span> <span style="color: #666666; font-style: italic;">// Set display id.</span>         <span style="color: #000088;">$view</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setDisplay</span><span style="color: #009900;">(</span><span style="color: #000088;">$display_id</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Get translated title.</span>         <span style="color: #000088;">$variables</span><span style="color: #009900;">[</span><span style="color: #0000ff;">'view_title'</span><span style="color: #009900;">]</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$view</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getTitle</span><span style="color: #009900;">(</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>       <span style="color: #009900;">}</span>     <span style="color: #009900;">}</span>   <span style="color: #009900;">}</span> <span style="color: #009900;">}</span></pre></div> <p>Als erstes besorgen wir uns ein <a href="https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Routing%21CurrentRouteMatch.php/class/CurrentRouteMatch/8.2.x">RouteMatch-Objekt</a> und rufen das aktuelle <a href="https://api.drupal.org/api/drupal/vendor%21symfony%21routing%21Route.php/class/Route/8.2.x">Route-Objekt</a> von dort ab. Dieses Route-Objekt ist das der gerade aktiven Route. Sobald wir das Route-Objekt haben, können wir es ausnutzen, dass Views diverse Standardwerte und Optionen auf die Routen setzt, die es zur Verfügung stellt. Auf diese Weise können wir die View-ID und die Display-ID des Views erhalten, der mit dieser Route assoziiert ist. Schau dir die Methode <a href="https://api.drupal.org/api/drupal/core%21modules%21views%21src%21Plugin%21views%21display%21PathPluginBase.php/function/PathPluginBase%3A%3AgetRoute/8.2.x">PathPluginBase::getRoute</a> an, um einen Überblick über weitere Standard-Werte und Optionen, die der Route hinzugefügt werden, zu erhalten, wenn du an etwas exotischeren Dingen interessiert bist ;).</p> <p>Sobald wir die View-ID und die Display-ID haben, ist es sehr einfach, das <a href="https://api.drupal.org/api/drupal/core%21modules%21views%21src%21ViewExecutable.php/class/ViewExecutable/8.2.x">View-Objekt</a> an Hand der View ID zu laden und das Display an Hand der Display ID passend zu setzen. Da wir in diesem Beispiel an dem Titel des Views interessiert sind, rufen wir schließlich die Methode <span class="geshifilter"><code class="php geshifilter-php">getTitle<span style="color: #009900;">(</span><span style="color: #009900;">)</span></code></span> des View-Objekts auf, um an den übersetzten Titel des Views zu kommen.</p> </div> <div class="field field--name-field-systems field--type-entity-reference field--label-inline"> <div class="field--label">Systeme</div> <a href="/de/systems/drupal-8" class="label label-default" hreflang="de">Drupal 8</a> </div> <div class="field field--name-field-tags field--type-entity-reference field--label-inline"> <div class="field--label">Tags</div> <a href="/en/tags/viewexecutable" class="label label-default" hreflang="en">ViewExecutable</a> <a href="/en/tags/route-match" class="label label-default" hreflang="en">route match</a> <a href="/de/tags/views" class="label label-default" hreflang="de">Views</a> <a href="/de/tags/preprocess" class="label label-default" hreflang="de">preprocess</a> <a href="/en/tags/currentroutematch" class="label label-default" hreflang="en">CurrentRouteMatch</a> <a href="/en/tags/route" class="label label-default" hreflang="en">Route</a> </div> Sun, 29 Jan 2017 12:26:29 +0000 patrick 110 at https://blog.werk21.de