Drupal մոդուլում թեմավորման տեմպլատի ավելացում
Մաս III՝ պրակտիկ ուղեցույց Drupal 8-ի բազային մոդուլներ ստեղծելու համար
.info ֆայլից դեպի թեստեր, միայն հիմունքներ
loremipsum.module
/** * Կատարվում է hook_theme()։ */ function loremipsum_theme($existing, $type, $theme, $path) { $variables = array( 'loremipsum' => array( 'variables' => array( 'source_text' => NULL, ), 'template' => 'loremipsum', ), ); return $variables; }
Մյուս պատճառը, թե ինչու պետք չէ հրաժարվել .module ֆայլից, այն է, որ հենց այնտեղ է գտնվում hook_theme()-ը։ Սա աշխատում է գրեթե այնպես, ինչպես D7-ում՝ դուք հայտարարում եք զանգված, որը պարունակում է ձեր փոփոխականները և թեմպլատի ֆայլը, որը պետք է պահպանվի ճիշտ վայրում (թեմպլատների պանակում)՝ .html.twig ընդլայնմամբ։
Հետո, մինչ ռենդերինգի զանգվածը փոխանցելը Twig-ին, կարող եք կատարել նախնական որոշակի մշակում։ Հաջորդ hook-ը յուրաքանչյուր նախադասության վերջում պատահական կետադրություն է ավելացնում․
/** * Թեմպլատի նախապատրաստման ֆունկցիա Lorem ipsum-ի համար։ * * @param array $variables * Ասոցիատիվ զանգված, որը պարունակում է՝ * - source_text */ function template_preprocess_loremipsum(&$variables) { $punctuation = array('. ', '! ', '? ', '... ', ': ', '; '); for ($i = 0; $i < count($variables['source_text']); $i++) { $big_text = explode('. ', $variables['source_text'][$i]); for ($j = 0; $j < count($big_text) - 1; $j++) { $big_text[$j] .= $punctuation[floor(mt_rand(0, count($punctuation) - 1))]; } $variables['source_text'][$i] = implode('', $big_text); } }
/templates/loremipsum.html.twig
{# /** * @file * Լռելի թեմայի իրականացում Lorem ipsum տեքստը տպելու համար։ * * Հասանելի փոփոխականներ՝ * - source_text * * @see template_preprocess_loremipsum() * * @ingroup themeable */ #} <div class="loremipsum"> {% for item in source_text %} <p>{{ item }}</p> {% endfor %} </div>
Հիմա $source_text զանգվածը մշակվում է սովորական for ցիկլի միջոցով մեր թեմի մեջ, որը շրջապատված է <p> թեգերով։
Նկատեք համապատասխանությունը hook_theme(), template_preprocess_hook() և մեր Twig ֆայլի միջև․
Drupal’s online documentation is © 2000-2020 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.