블록에 클래스 속성을 추가하는 방법은 무엇입니까?

Drupal 8 블록에 클래스 속성을 어떻게 추가 할 수 있나요?

Drupal 코어에는 도움말 모듈과 같은 많은 예가 있습니다. 블록의 역할 속성을 설정합니다. 그래서 성공적으로 클래스를 추가 할 수 있습니다.

/** * Implements hook_preprocess_HOOK() for block templates. */ function mymodule_preprocess_block(&$variables) { if ($variables["plugin_id"] == "mymodule_my_block") { $variables["attributes"]["class"][] = "my-nice-block"; } } 

전처리 후크없이 클래스를 추가 할 수 있습니까? 이상적으로는 src / Plugin / Block /에있는 블록 플러그인 파일 내에 있습니까?

이것은 한 블록에 관련된 모든 코드를 한 위치에 보관하는 데 도움이됩니다.

Answer

블록 플러그인이 반환하는 렌더 배열에 #attributes를 설정해 보셨습니까?

$build["#attributes"]["class"][] = "my-nice-block"; 

BlockViewBuilder::preRender()를 보면 작동합니다.

답변

블록 클래스 모듈을 확인할 수 있습니다.

Block Class를 사용하면 사용자가 블록의 구성 인터페이스를 통해 모든 블록에 클래스를 추가 할 수 있습니다. 테마의 block.tpl.php에 매우 짧은 PHP 스 니펫을 추가하여 파일, 클래스는 블록의 부모 요소에 추가 할 수 있습니다. 더 강력한 블록 테마를위한 만세!

답변

나뭇 가지 템플릿 찾기 블록에 대한 이름 제안 (twig 디버그 주석 사용) 및 재정의

themes / templates / block–my-custom-block.html.twig

{% extends "block.html.twig" %} {# /** * @file * Theme override for tabs. */ #} {% block title %} {{ title_prefix }} {% if label %} <h2{{ title_attributes.addClass(title_classes, "accordion") }}{{ title_attributes.setAttribute("id", "campus") }}> {{ label }} <i class="fa fa-plus" aria-hidden="true"></i> </h2> {% endif %} {{ title_suffix }} {% endblock %} 

답변

1. hello.config 파일을 만듭니다.

 my_module_name/config/install/my_module_name.config content this code: color :"blue" color_red: "red" 

2. 라이브러리 파일 my_module_name.libraries.yml

 // This function below is located on my_module_name.module file function my_module_name_page_attachments(array &$page){ $page["#attached"]["library"][]= "my_module_name/library_name"; } 

3. my_module_name.module 파일에서 클래스 설정

function my_module_name_preprocess_block (array & $ variables) {

// Get the value of my color content in my_module_name/config/install/my_module_name.config $class = \Drupal::config("my_module_name.config")->get("color"); 

// 블록 ID 가져 오기

 $id= $variables["elements"]["#id"]; $block = Block::load($id); 

// 블록

 $region = $block->getRegion(); if($region == "name_of_the_block_id"){ // Set the class $variables["attributes"]["class"][] = $class ; } } 

4. my_module_name.libraries.yml

여기서 my_module_name은 내 라이브러리의 이름입니다.

여기에 이미지 설명 입력

5. CSS 파일 css / my_module_name.css

.red {background : red! important; }

.green {background : green! important; }

.yellow {배경 : 노란색! important; }

답글 남기기

이메일 주소를 발행하지 않을 것입니다. 필수 항목은 *(으)로 표시합니다