Ver3.0 ๐ Drupal ๋ชจ๋ ๊ฐ๋ฐ๊ณผ ์ํฐํ๋ผ์ด์ฆ๊ธ CMS ์ปค์คํฐ๋ง์ด์ง

๐ Drupal ๋ชจ๋ ๊ฐ๋ฐ๊ณผ ์ํฐํ๋ผ์ด์ฆ๊ธ CMS ์ปค์คํฐ๋ง์ด์ง
์๋ ! ๐ ์ค๋์ PHP ๊ฐ๋ฐ์๋ผ๋ฉด ํ ๋ฒ์ฏค ๋ค์ด๋ดค์ Drupal์ ๋ํด ๊น์ด ์๊ฒ ํํค์ณ๋ณผ ๊ฑฐ์ผ. ํนํ ์ํฐํ๋ผ์ด์ฆ๊ธ ํ๋ก์ ํธ์์ Drupal์ ์ด๋ป๊ฒ ์ปค์คํฐ๋ง์ด์งํ๊ณ , ๋ชจ๋์ ๊ฐ๋ฐํ๋์ง ์ค์ ๋ ธํ์ฐ๋ฅผ ๊ณต์ ํด๋ณผ๊ฒ. ๐ฏ
Drupal์ ๋จ์ํ CMS๊ฐ ์๋์ผ. ์ด๊ฑด ๋ง์น ๋ ๊ณ ๋ธ๋ก ๊ฐ์ ์กด์ฌ๋ผ๊ณ ํ ๊น? ๊ธฐ๋ณธ ๋ธ๋ก๋ค์ ์กฐํฉํด์ ์ํ๋ ํํ์ ์น์ฌ์ดํธ๋ฅผ ๋ง๋ค ์ ์๋ ๊ฐ๋ ฅํ ํ๋ ์์ํฌ์ง. ํนํ ๋๊ท๋ชจ ๊ธฐ์ ์ฌ์ดํธ๋ ์ ๋ถ ๊ธฐ๊ด ์น์ฌ์ดํธ์์ ๋ง์ด ์ฌ์ฉ๋๋๋ฐ, ๊ทธ๋งํผ ํ์ฅ์ฑ๊ณผ ๋ณด์์ฑ์ด ๋ฐ์ด๋๋ค๋ ๋ป์ด์ผ. ๐ช
๐จ Drupal์ด ๋ญ๊ธธ๋ ์ด๋ ๊ฒ ๋๋ฆฌ์ผ?
๋จผ์ Drupal์ ์ ์ฒด๋ถํฐ ์์๋ณด์. Drupal์ 2001๋ ์ ๋ฒจ๊ธฐ์ ๊ฐ๋ฐ์ Dries Buytaert๊ฐ ๋ง๋ ์คํ์์ค CMS์ผ. 20๋ ์ด ๋๋ ์ญ์ฌ๋ฅผ ๊ฐ์ง๊ณ ์๊ณ , ํ์ฌ๋ Drupal 10๊น์ง ๋์จ ์ํ์ง. ๐
2. ๊ฐ๋ ฅํ ๊ถํ ๊ด๋ฆฌ: ์ธ๋ฐํ ์ฌ์ฉ์ ๊ถํ ์ค์ ์ด ๊ฐ๋ฅํด์ ์ํฐํ๋ผ์ด์ฆ์ ์ต์ ํ๋์ด ์์ง.
3. ๋ค๊ตญ์ด ์ง์: ๊ธฐ๋ณธ์ ์ผ๋ก 100๊ฐ ์ด์์ ์ธ์ด๋ฅผ ์ง์ํด.
4. RESTful API: ํค๋๋ฆฌ์ค CMS๋ก๋ ํ์ฉ ๊ฐ๋ฅํด.
5. ๋ณด์์ฑ: ์ ๊ธฐ์ ์ธ ๋ณด์ ์ ๋ฐ์ดํธ์ ๊ฐ๋ ฅํ ๋ณด์ ํ์ด ์์ด.
WordPress์ ๋น๊ตํ๋ฉด ์ด๋จ๊น? WordPress๋ ๋ธ๋ก๊ทธ์ ์ต์ ํ๋์ด ์๊ณ ์ฌ์ฉํ๊ธฐ ์ฌ์ด ๋ฐ๋ฉด, Drupal์ ๋ณต์กํ ๋น์ฆ๋์ค ๋ก์ง๊ณผ ๋๊ท๋ชจ ์ฝํ ์ธ ๊ด๋ฆฌ์ ๊ฐ์ ์ด ์์ด. ๋ง์น ์์ ๊ฑฐ์ ํธ๋ญ์ ๋น๊ตํ๋ ๊ฒ๊ณผ ๋น์ทํด. ์ฉ๋๊ฐ ๋ค๋ฅธ ๊ฑฐ์ง! ๐ฒ๐
์ค์ ๋ก NASA, ํ ์ฌ๋ผ, ํ์ด์ ๊ฐ์ ๊ธ๋ก๋ฒ ๊ธฐ์ ๋ค์ด Drupal์ ์ฌ์ฉํ๊ณ ์์ด. ์์ผ๊น? ๋ฐ๋ก ํ์ฅ์ฑ, ๋ณด์์ฑ, ์ ์ฐ์ฑ ๋๋ฌธ์ด์ผ.
๐ ๏ธ Drupal ๊ฐ๋ฐ ํ๊ฒฝ ์ธํ ํ๊ธฐ
๋ณธ๊ฒฉ์ ์ผ๋ก ๋ชจ๋ ๊ฐ๋ฐ์ ๋ค์ด๊ฐ๊ธฐ ์ ์, ๊ฐ๋ฐ ํ๊ฒฝ๋ถํฐ ์ ๋๋ก ๊ฐ์ถฐ์ผ ํด. ์๋ฆฌ๋ฅผ ํ๋ ค๋ฉด ์ฃผ๋ฐฉ๋ถํฐ ์ ๋ฆฌํด์ผ ํ๋ ๊ฒ์ฒ๋ผ ๋ง์ด์ผ! ๐ณ
๐ฆ ํ์ํ ๋๊ตฌ๋ค
โข Composer (์์กด์ฑ ๊ด๋ฆฌ ๋๊ตฌ)
โข MySQL 5.7.8+ ๋๋ MariaDB 10.3.7+
โข Apache 2.4+ ๋๋ Nginx
โข Drush (Drupal ์ปค๋งจ๋๋ผ์ธ ๋๊ตฌ)
Composer๋ PHP์ npm ๊ฐ์ ์กด์ฌ์ผ. Drupal 8๋ถํฐ๋ Composer๊ฐ ํ์๊ฐ ๋์์ด. ์์ ์๋ FTP๋ก ํ์ผ ์ ๋ก๋ํ๋ ์์ ๋ ์์์ง๋ง, ์ด์ ๋ ๊ทธ๋ฐ ์์์ ์ธ ๋ฐฉ๋ฒ์ ์ฐ์ง ์์! ๐
# Drupal ํ๋ก์ ํธ ์์ฑ
composer create-project drupal/recommended-project my_drupal_site
# ๊ฐ๋ฐ์ฉ ๋ชจ๋ ์ค์น
composer require --dev drupal/devel drupal/admin_toolbar
# Drush ์ค์น
composer require drush/drush
# ์บ์ ํด๋ฆฌ์ด (๊ฐ๋ฐ ์ค ๊ฐ์ฅ ๋ง์ด ์ฐ๋ ๋ช
๋ น์ด!)
drush cr
Drush๋ ์ ๋ง ๊ฐ๋ ฅํ ๋๊ตฌ์ผ. GUI๋ก 10๋ฒ ํด๋ฆญํ ๊ฒ์ ํ ์ค์ ๋ช ๋ น์ด๋ก ํด๊ฒฐํ ์ ์๊ฑฐ๋ . ๊ฐ๋ฐ ์๋๊ฐ ๋ช ๋ฐฐ๋ ๋นจ๋ผ์ ธ! โก
๐ณ Docker๋ก ๊ฐ๋ฐ ํ๊ฒฝ ๊ตฌ์ถํ๊ธฐ
์์ฆ์ Docker๋ฅผ ์ฌ์ฉํ๋ ๊ฒ ๋์ธ์ผ. ํ์๋ค๊ณผ ๋์ผํ ํ๊ฒฝ์์ ์์ ํ ์ ์๊ณ , "๋ด ์ปดํจํฐ์์๋ ๋๋๋ฐ์?" ๊ฐ์ ์ํฉ์ ๋ฐฉ์งํ ์ ์์ง. ๐ญ
# docker-compose.yml ์์
version: '3.8'
services:
drupal:
image: drupal:10-apache
ports:
- "8080:80"
volumes:
- ./modules:/var/www/html/modules
- ./themes:/var/www/html/themes
environment:
- DRUPAL_DATABASE_HOST=mysql
- DRUPAL_DATABASE_NAME=drupal
- DRUPAL_DATABASE_USER=drupal
- DRUPAL_DATABASE_PASSWORD=drupal
mysql:
image: mysql:8.0
environment:
- MYSQL_DATABASE=drupal
- MYSQL_USER=drupal
- MYSQL_PASSWORD=drupal
- MYSQL_ROOT_PASSWORD=root
์ด๋ ๊ฒ ์ค์ ํ๋ฉด `docker-compose up` ํ ๋ฐฉ์ผ๋ก ์ ์ฒด ๊ฐ๋ฐ ํ๊ฒฝ์ด ๋ฌ๋ค! ์ ๋ง ํธ๋ฆฌํ์ง? ๐
๐งฉ Drupal ๋ชจ๋ ๊ฐ๋ฐ์ ์ธ๊ณ๋ก
์, ์ด์ ๋ณธ๊ฒฉ์ ์ผ๋ก ๋ชจ๋ ๊ฐ๋ฐ์ ๋ค์ด๊ฐ๋ณด์! Drupal ๋ชจ๋์ ํฌ๊ฒ ๋ ๊ฐ์ง๋ก ๋๋์ด:
1. ์ปจํธ๋ฆฌ๋ทฐํธ ๋ชจ๋ (Contributed Modules): Drupal.org์์ ๋ค์ด๋ก๋ํ ์ ์๋ ๊ณต๊ฐ ๋ชจ๋๋ค
2. ์ปค์คํ
๋ชจ๋ (Custom Modules): ์ฐ๋ฆฌ๊ฐ ์ง์ ๋ง๋๋ ๋ชจ๋๋ค
์ค๋์ ์ปค์คํ ๋ชจ๋์ ๋ง๋๋ ๋ฐฉ๋ฒ์ ์ง์ค์ ์ผ๋ก ์์๋ณผ ๊ฑฐ์ผ! ๐ป
๐ ์ฒซ ๋ฒ์งธ ๋ชจ๋ ๋ง๋ค๊ธฐ
๊ฐ๋จํ "Hello World" ๋ชจ๋๋ถํฐ ์์ํด๋ณด์. ๋ชจ๋ ์ด๋ฆ์ `custom_greeting`์ผ๋ก ํ ๊ฒ.
โโโ custom_greeting.info.yml
โโโ custom_greeting.module
โโโ custom_greeting.routing.yml
โโโ src/
ย ย ย ย โโโ Controller/
ย ย ย ย ย ย ย ย โโโ GreetingController.php
๋จผ์ `.info.yml` ํ์ผ์ ๋ง๋ค์ด์ผ ํด. ์ด๊ฑด ๋ชจ๋์ ์ ๋ถ์ฆ ๊ฐ์ ๊ฑฐ์ผ! ๐ชช
# custom_greeting.info.yml
name: Custom Greeting
type: module
description: '์ฌ์ฉ์์๊ฒ ์ธ์ฌ๋ง์ ํ์ํ๋ ์ปค์คํ
๋ชจ๋'
core_version_requirement: ^9 || ^10
package: Custom
dependencies:
- drupal:node
- drupal:user
๋ค์์ ๋ผ์ฐํ ํ์ผ์ด์ผ. URL๊ณผ ์ปจํธ๋กค๋ฌ๋ฅผ ์ฐ๊ฒฐํด์ฃผ๋ ์ญํ ์ ํด:
# custom_greeting.routing.yml
custom_greeting.hello:
path: '/hello/{name}'
defaults:
_controller: '\Drupal\custom_greeting\Controller\GreetingController::hello'
_title: '์ธ์ฌ๋ง'
name: 'Guest'
requirements:
_permission: 'access content'
options:
parameters:
name:
type: string
์ด์ ์ค์ ๋ก์ง์ ๋ด๋นํ ์ปจํธ๋กค๋ฌ๋ฅผ ๋ง๋ค์ด๋ณด์:
<?php
namespace Drupal\custom_greeting\Controller;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpFoundation\Response;
/**
* ์ธ์ฌ๋ง ์ปจํธ๋กค๋ฌ
*/
class GreetingController extends ControllerBase {
/**
* ์ฌ์ฉ์์๊ฒ ์ธ์ฌ๋ง์ ํ์
*
* @param string $name
* ์ฌ์ฉ์ ์ด๋ฆ
*
* @return array
* ๋ ๋ ๋ฐฐ์ด
*/
public function hello($name = 'Guest') {
// ํ์ฌ ์๊ฐ์ ๋ฐ๋ผ ๋ค๋ฅธ ์ธ์ฌ๋ง ํ์
$hour = date('H');
if ($hour < 12) {
$greeting = $this->t('Good morning');
} elseif ($hour < 18) {
$greeting = $this->t('Good afternoon');
} else {
$greeting = $this->t('Good evening');
}
return [
'#markup' => $this->t('@greeting, @name! Welcome to our site.', [
'@greeting' => $greeting,
'@name' => $name,
]),
];
}
}
์ง ! ๐ ์ด์ `/hello/John`์ผ๋ก ์ ์ํ๋ฉด "Good morning, John! Welcome to our site." ๊ฐ์ ๋ฉ์์ง๊ฐ ํ์๋ ๊ฑฐ์ผ. ์๊ฐ๋์ ๋ฐ๋ผ ์ธ์ฌ๋ง์ด ๋ฐ๋๋ ์ผ์ค๊น์ง!
๐ฃ Hook ์์คํ ๋ง์คํฐํ๊ธฐ
Drupal์ ์ง์ง ํ์๋ Hook ์์คํ ์ ์์ด! Hook์ Drupal ์ฝ์ด๋ ๋ค๋ฅธ ๋ชจ๋์ ๋์์ ๋ผ์ด๋ค ์ ์๊ฒ ํด์ฃผ๋ ๋ฉ์ปค๋์ฆ์ด์ผ. ๋ง์น ์ํ ์ค๊ฐ์ ๊ด๊ณ ๋ฅผ ๋ผ์๋ฃ๋ ๊ฒ์ฒ๋ผ! ๐บ
๊ฐ์ฅ ๋ง์ด ์ฌ์ฉ๋๋ Hook๋ค์ ์์๋ณด์:
<?php
/**
* Implements hook_form_alter().
*
* ๋ชจ๋ ํผ์ ์์ ํ ์ ์์ด
*/
function custom_greeting_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
// ์ฌ์ฉ์ ๋ก๊ทธ์ธ ํผ์ ๋์๋ง ์ถ๊ฐ
if ($form_id == 'user_login_form') {
$form['help_text'] = [
'#markup' => '<p>๋ก๊ทธ์ธ์ ๋ฌธ์ ๊ฐ ์์ผ์ ๊ฐ์? ๊ณ ๊ฐ์ผํฐ์ ๋ฌธ์ํ์ธ์!</p>',
'#weight' => -10,
];
}
}
/**
* Implements hook_node_presave().
*
* ๋
ธ๋๊ฐ ์ ์ฅ๋๊ธฐ ์ง์ ์ ์คํ๋ผ
*/
function custom_greeting_node_presave(\Drupal\node\NodeInterface $node) {
// ์ ๋ชฉ์ด ๋น์ด์์ผ๋ฉด ์๋์ผ๋ก ์์ฑ
if (empty($node->getTitle())) {
$node->setTitle('์ ๋ชฉ ์์ - ' . date('Y-m-d H:i:s'));
}
// ํน์ ๋จ์ด ํํฐ๋ง
$title = $node->getTitle();
$bad_words = ['spam', 'advertisement'];
foreach ($bad_words as $word) {
if (stripos($title, $word) !== FALSE) {
\Drupal::messenger()->addWarning('์ ๋ชฉ์ ๋ถ์ ์ ํ ๋จ์ด๊ฐ ํฌํจ๋์ด ์์ต๋๋ค.');
}
}
}
/**
* Implements hook_cron().
*
* ํฌ๋ก ์์
์ ์คํ๋ผ
*/
function custom_greeting_cron() {
// 30์ผ ์ด์ ๋ ๋ก๊ทธ ์ญ์
$connection = \Drupal::database();
$connection->delete('custom_greeting_logs')
->condition('created', strtotime('-30 days'), '<')
->execute();
\Drupal::logger('custom_greeting')->info('์ค๋๋ ๋ก๊ทธ๋ฅผ ์ ๋ฆฌํ์ต๋๋ค.');
}
Hook์ ์ ๋ง ๋ค์ํด. `hook_install()`, `hook_uninstall()`, `hook_entity_view()`, `hook_theme()` ๋ฑ๋ฑ... ์๋ฐฑ ๊ฐ๊ฐ ๋์ด! ๐ฑ
๐๏ธ ์ํฐํ๋ผ์ด์ฆ๊ธ ์ํคํ ์ฒ ์ค๊ณ
์ด์ ์ง์ง ์ค์ ์ผ๋ก ๋ค์ด๊ฐ๋ณด์! ์ํฐํ๋ผ์ด์ฆ ํ๋ก์ ํธ๋ ๊ฐ์ธ ๋ธ๋ก๊ทธ ๋ง๋๋ ๊ฒ๊ณผ๋ ์ฐจ์์ด ๋ฌ๋ผ. ์๋ฐฑ๋ง ๋ช ์ ์ฌ์ฉ์, ์์ฒ๋ง ๊ฐ์ ์ฝํ ์ธ , ๋ณต์กํ ๋น์ฆ๋์ค ๋ก์ง์ ๋ค๋ค์ผ ํ๊ฑฐ๋ . ๐ข
๐ฏ ์๋น์ค ์ปจํ ์ด๋์ ์์กด์ฑ ์ฃผ์
Drupal 8๋ถํฐ๋ Symfony ์ปดํฌ๋ํธ๋ฅผ ์ฌ์ฉํด์ ์์กด์ฑ ์ฃผ์ (DI) ํจํด์ ์ง์ํด. ์ด๊ฒ ๋ญ๋๊ณ ? ๊ฐ๋จํ ๋งํ๋ฉด ๊ฐ์ฒด๋ฅผ ์ง์ ์์ฑํ์ง ์๊ณ ์ธ๋ถ์์ ์ฃผ์ ๋ฐ๋ ๊ฑฐ์ผ. ์ฝ๋์ ๊ฒฐํฉ๋๋ฅผ ๋ฎ์ถ๊ณ ํ ์คํธํ๊ธฐ ์ฝ๊ฒ ๋ง๋ค์ด์ฃผ์ง! ๐
<?php
namespace Drupal\custom_greeting\Service;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
/**
* ์ฌ์ฉ์ ์ธ์ฌ๋ง ์๋น์ค
*/
class GreetingService {
/**
* ์ํฐํฐ ํ์
๋งค๋์
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* ํ์ฌ ์ฌ์ฉ์
*
* @var \Drupal\Core\Session\AccountProxyInterface
*/
protected $currentUser;
/**
* ๋ก๊ฑฐ
*
* @var \Drupal\Core\Logger\LoggerChannelInterface
*/
protected $logger;
/**
* ์์ฑ์
*/
public function __construct(
EntityTypeManagerInterface $entity_type_manager,
AccountProxyInterface $current_user,
LoggerChannelFactoryInterface $logger_factory
) {
$this->entityTypeManager = $entity_type_manager;
$this->currentUser = $current_user;
$this->logger = $logger_factory->get('custom_greeting');
}
/**
* ๊ฐ์ธํ๋ ์ธ์ฌ๋ง ์์ฑ
*
* @return string
* ์ธ์ฌ๋ง ๋ฌธ์์ด
*/
public function getPersonalizedGreeting() {
$uid = $this->currentUser->id();
if ($uid > 0) {
$user_storage = $this->entityTypeManager->getStorage('user');
$user = $user_storage->load($uid);
$name = $user->getDisplayName();
$login_count = $this->getLoginCount($uid);
$this->logger->info('์ฌ์ฉ์ @name์๊ฒ ์ธ์ฌ๋ง ํ์', ['@name' => $name]);
if ($login_count == 1) {
return "ํ์ํฉ๋๋ค, {$name}๋! ์ฒซ ๋ฐฉ๋ฌธ์ ์ถํ๋๋ฆฝ๋๋ค! ๐";
} else {
return "{$name}๋, ๋ค์ ์ค์ ๊ฒ์ ํ์ํฉ๋๋ค! ({$login_count}๋ฒ์งธ ๋ฐฉ๋ฌธ)";
}
}
return "์๋
ํ์ธ์! ๋ก๊ทธ์ธํ์๋ฉด ๋ ๋ง์ ๊ธฐ๋ฅ์ ์ด์ฉํ์ค ์ ์์ต๋๋ค.";
}
/**
* ๋ก๊ทธ์ธ ํ์ ์กฐํ
*/
protected function getLoginCount($uid) {
$connection = \Drupal::database();
$query = $connection->select('custom_greeting_login_log', 'l')
->condition('uid', $uid)
->countQuery();
return $query->execute()->fetchField();
}
}
์ด์ ์ด ์๋น์ค๋ฅผ ๋ฑ๋กํด์ผ ํด. `custom_greeting.services.yml` ํ์ผ์ ๋ง๋ค์:
services:
custom_greeting.greeting_service:
class: Drupal\custom_greeting\Service\GreetingService
arguments:
- '@entity_type.manager'
- '@current_user'
- '@logger.factory'
์ด์ ์ด๋์๋ ์ด ์๋น์ค๋ฅผ ์ฌ์ฉํ ์ ์์ด:
// ์ปจํธ๋กค๋ฌ์์ ์ฌ์ฉ
$greeting_service = \Drupal::service('custom_greeting.greeting_service');
$message = $greeting_service->getPersonalizedGreeting();
์์กด์ฑ ์ฃผ์ ์ ์ฌ์ฉํ๋ฉด ์ฝ๋๊ฐ ํจ์ฌ ๊น๋ํด์ง๊ณ , ๋จ์ ํ ์คํธ๋ ์ฌ์์ ธ. Mock ๊ฐ์ฒด๋ฅผ ์ฃผ์ ํด์ ํ ์คํธํ ์ ์๊ฑฐ๋ ! ๐งช
๐๏ธ ์ปค์คํ ์ํฐํฐ ํ์ ๋ง๋ค๊ธฐ
Drupal์ ๋ชจ๋ ๊ฒ์ ์ํฐํฐ์ผ. ๋ ธ๋, ์ฌ์ฉ์, ๋๊ธ, ํ์๋ ธ๋ฏธ... ๋ค ์ํฐํฐ์ง. ์ฐ๋ฆฌ๋ ์ปค์คํ ์ํฐํฐ๋ฅผ ๋ง๋ค ์ ์์ด! ์๋ฅผ ๋ค์ด "์ด๋ฒคํธ" ์ํฐํฐ๋ฅผ ๋ง๋ค์ด๋ณด์. ๐
<?php
namespace Drupal\custom_greeting\Entity;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
/**
* ์ด๋ฒคํธ ์ํฐํฐ
*
* @ContentEntityType(
* id = "custom_event",
* label = @Translation("Event"),
* base_table = "custom_event",
* entity_keys = {
* "id" = "id",
* "uuid" = "uuid",
* "label" = "title",
* },
* handlers = {
* "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
* "list_builder" = "Drupal\custom_greeting\EventListBuilder",
* "form" = {
* "default" = "Drupal\custom_greeting\Form\EventForm",
* "add" = "Drupal\custom_greeting\Form\EventForm",
* "edit" = "Drupal\custom_greeting\Form\EventForm",
* "delete" = "Drupal\Core\Entity\ContentEntityDeleteForm",
* },
* "route_provider" = {
* "html" = "Drupal\Core\Entity\Routing\AdminHtmlRouteProvider",
* },
* },
* links = {
* "canonical" = "/event/{custom_event}",
* "add-form" = "/admin/content/event/add",
* "edit-form" = "/admin/content/event/{custom_event}/edit",
* "delete-form" = "/admin/content/event/{custom_event}/delete",
* "collection" = "/admin/content/events",
* },
* )
*/
class Event extends ContentEntityBase {
/**
* {@inheritdoc}
*/
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
$fields['title'] = BaseFieldDefinition::create('string')
->setLabel(t('Title'))
->setDescription(t('์ด๋ฒคํธ ์ ๋ชฉ'))
->setRequired(TRUE)
->setSettings([
'max_length' => 255,
'text_processing' => 0,
])
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'string',
'weight' => -5,
])
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => -5,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['description'] = BaseFieldDefinition::create('text_long')
->setLabel(t('Description'))
->setDescription(t('์ด๋ฒคํธ ์ค๋ช
'))
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'text_default',
'weight' => 0,
])
->setDisplayOptions('form', [
'type' => 'text_textarea',
'weight' => 0,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['start_date'] = BaseFieldDefinition::create('datetime')
->setLabel(t('Start Date'))
->setDescription(t('์ด๋ฒคํธ ์์์ผ'))
->setRequired(TRUE)
->setSettings([
'datetime_type' => 'datetime',
])
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'datetime_default',
'weight' => 1,
])
->setDisplayOptions('form', [
'type' => 'datetime_default',
'weight' => 1,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['end_date'] = BaseFieldDefinition::create('datetime')
->setLabel(t('End Date'))
->setDescription(t('์ด๋ฒคํธ ์ข
๋ฃ์ผ'))
->setRequired(TRUE)
->setSettings([
'datetime_type' => 'datetime',
])
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'datetime_default',
'weight' => 2,
])
->setDisplayOptions('form', [
'type' => 'datetime_default',
'weight' => 2,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Created'))
->setDescription(t('์์ฑ ์๊ฐ'));
$fields['changed'] = BaseFieldDefinition::create('changed')
->setLabel(t('Changed'))
->setDescription(t('์์ ์๊ฐ'));
return $fields;
}
}
์! ์ฝ๋๊ฐ ์ข ๊ธธ์ง? ๐ ํ์ง๋ง ์ด๋ ๊ฒ ํ ๋ฒ๋ง ์ ์ํด๋๋ฉด, Drupal์ด ์๋์ผ๋ก CRUD ์ธํฐํ์ด์ค, ๋ฐ์ดํฐ๋ฒ ์ด์ค ํ ์ด๋ธ, REST API ์๋ํฌ์ธํธ๊น์ง ๋ง๋ค์ด์ค! ์ ๋ง ํธ๋ฆฌํ์ง?
โข ํ๋ API ํตํฉ
โข ๋ทฐ(Views) ํตํฉ
โข REST API ์๋ ์์ฑ
โข ๊ถํ ๊ด๋ฆฌ ์์คํ ํตํฉ
โข ๋ฒ์ญ ์ง์
โข ๋ฆฌ๋น์ (๋ฒ์ ๊ด๋ฆฌ) ์ง์
โก ์ฑ๋ฅ ์ต์ ํ ์ ๋ต
์ํฐํ๋ผ์ด์ฆ ์ฌ์ดํธ์์ ์ฑ๋ฅ์ ์๋ช ์ด์ผ! ๋๋ฆฐ ์ฌ์ดํธ๋ ์ฌ์ฉ์๋ฅผ ์๊ณ , ๋งค์ถ๋ ๋จ์ด๋จ๋ฆฌ๊ฑฐ๋ . Drupal์ ๊ฐ๋ ฅํ์ง๋ง, ์๋ชป ์ฌ์ฉํ๋ฉด ๋๋ ค์ง ์ ์์ด. ์ต์ ํ ๋น๋ฒ์ ์๋ ค์ค๊ฒ! ๐
๐พ ์บ์ฑ ์ ๋ต
Drupal์ ์บ์ฑ ์์คํ ์ ์ ๋ง ๊ฐ๋ ฅํด. ์ฌ๋ฌ ๋ ์ด์ด๋ก ๊ตฌ์ฑ๋์ด ์์ง:
| ์บ์ ํ์ | ์ค๋ช | ์ฌ์ฉ ์๊ธฐ |
|---|---|---|
| ํ์ด์ง ์บ์ | ์ต๋ช ์ฌ์ฉ์์ฉ ์ ์ฒด ํ์ด์ง ์บ์ฑ | ํญ์ ํ์ฑํ |
| ๋์ ํ์ด์ง ์บ์ | ๋ก๊ทธ์ธ ์ฌ์ฉ์์ฉ ๋ถ๋ถ ์บ์ฑ | ํญ์ ํ์ฑํ |
| ๋ ๋ ์บ์ | ๊ฐ๋ณ ๋ ๋ ์์ ์บ์ฑ | ๋ฌด๊ฑฐ์ด ์ฐ์ฐ์ ์ฌ์ฉ |
| ๋ฐ์ดํฐ ์บ์ | ์ปค์คํ ๋ฐ์ดํฐ ์บ์ฑ | API ์๋ต, ๊ณ์ฐ ๊ฒฐ๊ณผ ๋ฑ |
์ปค์คํ ์บ์ฑ์ ๊ตฌํํด๋ณด์:
<?php
/**
* ๋ฌด๊ฑฐ์ด ๊ณ์ฐ ๊ฒฐ๊ณผ๋ฅผ ์บ์ฑ
*/
function getExpensiveData() {
$cid = 'custom_greeting:expensive_data';
$cache = \Drupal::cache()->get($cid);
if ($cache) {
// ์บ์๊ฐ ์์ผ๋ฉด ๋ฐ๋ก ๋ฐํ
return $cache->data;
}
// ์บ์๊ฐ ์์ผ๋ฉด ๊ณ์ฐ
$data = performExpensiveCalculation();
// 1์๊ฐ ๋์ ์บ์ฑ
\Drupal::cache()->set($cid, $data, time() + 3600, ['custom_greeting']);
return $data;
}
/**
* ๋ ๋ ๋ฐฐ์ด์ ์บ์ ๋ฉํ๋ฐ์ดํฐ ์ถ๊ฐ
*/
function buildCachedBlock() {
$build = [
'#markup' => '<div>์บ์๋ ์ฝํ
์ธ </div>',
'#cache' => [
'keys' => ['custom_greeting', 'cached_block'],
'contexts' => ['user', 'languages'],
'tags' => ['custom_greeting:block'],
'max-age' => 3600, // 1์๊ฐ
],
];
return $build;
}
๐๏ธ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฟผ๋ฆฌ ์ต์ ํ
N+1 ์ฟผ๋ฆฌ ๋ฌธ์ ๋ ์ฑ๋ฅ์ ์ ์ด์ผ! Entity API๋ฅผ ์ฌ์ฉํ ๋ ํนํ ์กฐ์ฌํด์ผ ํด:
<?php
// โ ๋์ ์: N+1 ์ฟผ๋ฆฌ ๋ฌธ์
$nids = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
foreach ($nids as $nid) {
$node = \Drupal::entityTypeManager()
->getStorage('node')
->load($nid); // 10๋ฒ์ ์ฟผ๋ฆฌ!
echo $node->getTitle();
}
// โ
์ข์ ์: ํ ๋ฒ์ ๋ก๋
$nodes = \Drupal::entityTypeManager()
->getStorage('node')
->loadMultiple($nids); // 1๋ฒ์ ์ฟผ๋ฆฌ!
foreach ($nodes as $node) {
echo $node->getTitle();
}
// โ
๋ ์ข์ ์: ํ์ํ ํ๋๋ง ์ฟผ๋ฆฌ
$query = \Drupal::database()->select('node_field_data', 'n')
->fields('n', ['nid', 'title'])
->condition('nid', $nids, 'IN')
->execute();
$titles = $query->fetchAllKeyed();
๋ฐ์ดํฐ๋ฒ ์ด์ค ์ธ๋ฑ์ค๋ ์์ง ๋ง์! ์ปค์คํ ํ ์ด๋ธ์ ๋ง๋ค ๋๋ ๋ฐ๋์ ์ธ๋ฑ์ค๋ฅผ ์ถ๊ฐํด์ผ ํด:
<?php
/**
* Implements hook_schema().
*/
function custom_greeting_schema() {
$schema['custom_greeting_logs'] = [
'description' => '์ธ์ฌ๋ง ๋ก๊ทธ',
'fields' => [
'id' => [
'type' => 'serial',
'not null' => TRUE,
'description' => '๊ธฐ๋ณธ ํค',
],
'uid' => [
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'description' => '์ฌ์ฉ์ ID',
],
'message' => [
'type' => 'text',
'not null' => TRUE,
'description' => '์ธ์ฌ๋ง ๋ด์ฉ',
],
'created' => [
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'description' => '์์ฑ ์๊ฐ',
],
],
'primary key' => ['id'],
'indexes' => [
'uid' => ['uid'],
'created' => ['created'],
'uid_created' => ['uid', 'created'], // ๋ณตํฉ ์ธ๋ฑ์ค
],
];
return $schema;
}
๐จ ํ๋ก ํธ์๋ ์ต์ ํ
๋ฐฑ์๋๋ง ๋น ๋ฅด๋ค๊ณ ๋์ด ์๋์ผ! ํ๋ก ํธ์๋ ์ต์ ํ๋ ์ค์ํด:
- โ CSS/JS ์ง๊ณ(Aggregation) ํ์ฑํ
- โ ์ด๋ฏธ์ง ์ต์ ํ (WebP, ์ง์ฐ ๋ก๋ฉ)
- โ CDN ์ฌ์ฉ
- โ HTTP/2 ํ์ฑํ
- โ Gzip ์์ถ
- โ ํฌ๋ฆฌํฐ์ปฌ CSS ์ธ๋ผ์ธ
- โ ๋ถํ์ํ ๋ชจ๋ ๋นํ์ฑํ
Drupal 10๋ถํฐ๋ ์๋์ผ๋ก ์ง์ฐ ๋ก๋ฉ์ด ์ ์ฉ๋์ง๋ง, ์ปค์คํ ์ด๋ฏธ์ง๋ ์ง์ ์ค์ ํด์ผ ํด:
<?php
$build['image'] = [
'#theme' => 'image',
'#uri' => 'public://my-image.jpg',
'#alt' => '์ด๋ฏธ์ง ์ค๋ช
',
'#attributes' => [
'loading' => 'lazy', // ์ง์ฐ ๋ก๋ฉ
'decoding' => 'async', // ๋น๋๊ธฐ ๋์ฝ๋ฉ
],
];
๐ ๋ณด์ ๋ฒ ์คํธ ํ๋ํฐ์ค
์ํฐํ๋ผ์ด์ฆ ์ฌ์ดํธ์์ ๋ณด์์ ํํํ ์ ์๋ ์์ญ์ด์ผ! Drupal์ ๊ธฐ๋ณธ์ ์ผ๋ก ๋ณด์์ด ์ ๋์ด ์์ง๋ง, ๊ฐ๋ฐ์๊ฐ ์ค์ํ๋ฉด ๊ตฌ๋ฉ์ด ์๊ธฐ์ง. ๐ฑ
๐ก๏ธ ์ ๋ ฅ ๊ฒ์ฆ๊ณผ ์ถ๋ ฅ ์ด์ค์ผ์ดํ
๊ฐ์ฅ ๊ธฐ๋ณธ์ ์ด๋ฉด์๋ ์ค์ํ ๋ณด์ ์์น์ด์ผ:
<?php
// โ ์ํํ ์ฝ๋: XSS ์ทจ์ฝ์
$name = $_GET['name'];
echo "<h1>Hello, $name!</h1>";
// โ
์์ ํ ์ฝ๋: ์ด์ค์ผ์ดํ
$name = \Drupal\Component\Utility\Html::escape($_GET['name']);
echo "<h1>Hello, $name!</h1>";
// โ
๋ ์ข์ ๋ฐฉ๋ฒ: ๋ ๋ ๋ฐฐ์ด ์ฌ์ฉ
$build = [
'#markup' => $this->t('Hello, @name!', ['@name' => $_GET['name']]),
];
// โ ์ํํ SQL ์ฟผ๋ฆฌ
$uid = $_GET['uid'];
$query = "SELECT * FROM users WHERE uid = $uid";
// โ
์์ ํ ์ฟผ๋ฆฌ: ํ๋ผ๋ฏธํฐ ๋ฐ์ธ๋ฉ
$uid = $_GET['uid'];
$query = \Drupal::database()->select('users_field_data', 'u')
->fields('u')
->condition('uid', $uid)
->execute();
// โ
Entity API ์ฌ์ฉ (๋ ์์ )
$user = \Drupal\user\Entity\User::load($uid);
โข `eval()` ํจ์ ์ฌ์ฉ
โข `unserialize()` ์ ๋ขฐํ ์ ์๋ ๋ฐ์ดํฐ์ ์ฌ์ฉ
โข ํ์ผ ์ ๋ก๋ ๊ฒ์ฆ ์๋ต
โข ๊ถํ ์ฒดํฌ ์์ด ๋ฏผ๊ฐํ ์์ ์ํ
๐ ๊ถํ ๊ด๋ฆฌ
Drupal์ ๊ถํ ์์คํ ์ ์ ๋ง ์ธ๋ฐํด. ์ปค์คํ ๊ถํ์ ๋ง๋ค์ด๋ณด์:
# custom_greeting.permissions.yml
administer custom greeting:
title: 'Administer custom greeting'
description: '์ธ์ฌ๋ง ๋ชจ๋ ๊ด๋ฆฌ'
restrict access: true
view greeting logs:
title: 'View greeting logs'
description: '์ธ์ฌ๋ง ๋ก๊ทธ ์กฐํ'
create custom events:
title: 'Create custom events'
description: '์ปค์คํ
์ด๋ฒคํธ ์์ฑ'
์ฝ๋์์ ๊ถํ ์ฒดํฌํ๊ธฐ:
<?php
// ํ์ฌ ์ฌ์ฉ์ ๊ถํ ์ฒดํฌ
if (\Drupal::currentUser()->hasPermission('administer custom greeting')) {
// ๊ด๋ฆฌ์ ์์
์ํ
}
// ์ปจํธ๋กค๋ฌ์์ ๊ถํ ์ฒดํฌ
public function adminPage() {
// ๋ผ์ฐํ
์์ _permission์ผ๋ก ์ฒดํฌํ๋ ๊ฒ ๋ ์ข์ง๋ง,
// ๋์ ์ผ๋ก ์ฒดํฌํด์ผ ํ ๋๋ ์ด๋ ๊ฒ
if (!$this->currentUser()->hasPermission('administer custom greeting')) {
throw new AccessDeniedHttpException();
}
// ๊ด๋ฆฌ ํ์ด์ง ๋ ๋๋ง
}
// ์ํฐํฐ ์ ๊ทผ ์ฒดํฌ
$node = Node::load(1);
if ($node->access('update')) {
// ์์ ๊ฐ๋ฅ
}
๐ ๋ณด์ ๊ฐ์ฌ
์ ๊ธฐ์ ์ธ ๋ณด์ ์ ๊ฒ์ด ํ์ํด! ์ด๋ฐ ๊ฒ๋ค์ ์ฒดํฌํด๋ด:
- โ ๋ชจ๋ ๋ชจ๋ ์ต์ ๋ฒ์ ์ ์ง
- โ ์ฌ์ฉํ์ง ์๋ ๋ชจ๋ ์ ๊ฑฐ
- โ ํ์ผ ๊ถํ ํ์ธ (sites/default/settings.php๋ ์ฝ๊ธฐ ์ ์ฉ)
- โ HTTPS ์ฌ์ฉ
- โ ๊ฐ๋ ฅํ ๋น๋ฐ๋ฒํธ ์ ์ฑ
- โ 2๋จ๊ณ ์ธ์ฆ ํ์ฑํ
- โ ์ ๊ธฐ์ ์ธ ๋ฐฑ์
- โ ๋ณด์ ๋ก๊ทธ ๋ชจ๋ํฐ๋ง
- โ Rate limiting ์ค์
Drupal Security Team์์ ์ ๊ณตํ๋ ๋ณด์ ์ ๋ฐ์ดํธ๋ ๋ฐ๋์ ์ ์ฉํด์ผ ํด! ๋ณดํต ์์์ผ์ ๋ฐํ๋๋๋ฐ, ์ด๊ฑธ "Security Wednesday"๋ผ๊ณ ๋ถ๋ฅด์ง. ๐
๐งช ํ ์คํธ์ ํ์ง ๊ด๋ฆฌ
์ํฐํ๋ผ์ด์ฆ ํ๋ก์ ํธ์์ ํ ์คํธ๋ ์ ํ์ด ์๋๋ผ ํ์์ผ! "๋ด ์ปดํจํฐ์์๋ ์ ๋๋๋ฐ์?"๋ผ๋ ๋ง์ ์ด์ ๊ทธ๋ง! ๐
๐ฏ PHPUnit ๋จ์ ํ ์คํธ
Drupal์ PHPUnit์ ๊ธฐ๋ณธ์ผ๋ก ์ง์ํด. ํ ์คํธ ์ฝ๋๋ฅผ ์์ฑํด๋ณด์:
<?php
namespace Drupal\Tests\custom_greeting\Unit;
use Drupal\Tests\UnitTestCase;
use Drupal\custom_greeting\Service\GreetingService;
/**
* ์ธ์ฌ๋ง ์๋น์ค ํ
์คํธ
*
* @group custom_greeting
*/
class GreetingServiceTest extends UnitTestCase {
/**
* ํ
์คํธํ ์๋น์ค
*
* @var \Drupal\custom_greeting\Service\GreetingService
*/
protected $greetingService;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
// Mock ๊ฐ์ฒด ์์ฑ
$entity_type_manager = $this->createMock('\Drupal\Core\Entity\EntityTypeManagerInterface');
$current_user = $this->createMock('\Drupal\Core\Session\AccountProxyInterface');
$logger_factory = $this->createMock('\Drupal\Core\Logger\LoggerChannelFactoryInterface');
$this->greetingService = new GreetingService(
$entity_type_manager,
$current_user,
$logger_factory
);
}
/**
* ์๊ฐ๋๋ณ ์ธ์ฌ๋ง ํ
์คํธ
*/
public function testTimeBasedGreeting() {
// ์์นจ ์๊ฐ ํ
์คํธ
$morning_greeting = $this->greetingService->getGreetingByHour(9);
$this->assertStringContainsString('morning', $morning_greeting);
// ์คํ ์๊ฐ ํ
์คํธ
$afternoon_greeting = $this->greetingService->getGreetingByHour(15);
$this->assertStringContainsString('afternoon', $afternoon_greeting);
// ์ ๋
์๊ฐ ํ
์คํธ
$evening_greeting = $this->greetingService->getGreetingByHour(20);
$this->assertStringContainsString('evening', $evening_greeting);
}
/**
* ๋น ์ด๋ฆ ์ฒ๋ฆฌ ํ
์คํธ
*/
public function testEmptyNameHandling() {
$greeting = $this->greetingService->createGreeting('');
$this->assertStringContainsString('Guest', $greeting);
}
/**
* ํน์ ๋ฌธ์ ์ด์ค์ผ์ดํ ํ
์คํธ
*/
public function testSpecialCharacterEscaping() {
$greeting = $this->greetingService->createGreeting('<script>alert("XSS")</script>');
$this->assertStringNotContainsString('<script>', $greeting);
}
}
ํ ์คํธ ์คํํ๊ธฐ:
# ์ ์ฒด ํ
์คํธ ์คํ
php core/scripts/run-tests.sh --verbose --color --module custom_greeting
# PHPUnit์ผ๋ก ์คํ
./vendor/bin/phpunit modules/custom/custom_greeting/tests/
# ํน์ ํ
์คํธ๋ง ์คํ
./vendor/bin/phpunit modules/custom/custom_greeting/tests/src/Unit/GreetingServiceTest.php
๐ ๊ธฐ๋ฅ ํ ์คํธ
์ค์ ๋ธ๋ผ์ฐ์ ์ฒ๋ผ ๋์ํ๋ ๊ธฐ๋ฅ ํ ์คํธ๋ ์ค์ํด:
<?php
namespace Drupal\Tests\custom_greeting\Functional;
use Drupal\Tests\BrowserTestBase;
/**
* ์ธ์ฌ๋ง ํ์ด์ง ๊ธฐ๋ฅ ํ
์คํธ
*
* @group custom_greeting
*/
class GreetingPageTest extends BrowserTestBase {
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* {@inheritdoc}
*/
protected static $modules = ['custom_greeting', 'node', 'user'];
/**
* ์ธ์ฌ๋ง ํ์ด์ง ์ ๊ทผ ํ
์คํธ
*/
public function testGreetingPageAccess() {
// ์ต๋ช
์ฌ์ฉ์๋ก ํ์ด์ง ์ ๊ทผ
$this->drupalGet('/hello/John');
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->pageTextContains('John');
// ๋ก๊ทธ์ธ ์ฌ์ฉ์๋ก ํ
์คํธ
$user = $this->drupalCreateUser(['access content']);
$this->drupalLogin($user);
$this->drupalGet('/hello/' . $user->getDisplayName());
$this->assertSession()->pageTextContains($user->getDisplayName());
}
/**
* ํผ ์ ์ถ ํ
์คํธ
*/
public function testGreetingForm() {
$admin = $this->drupalCreateUser(['administer custom greeting']);
$this->drupalLogin($admin);
$this->drupalGet('/admin/config/custom-greeting');
$edit = [
'default_greeting' => 'Welcome to our site!',
'show_time' => TRUE,
];
$this->submitForm($edit, 'Save configuration');
$this->assertSession()->pageTextContains('The configuration has been saved.');
}
}
๐ ์ฝ๋ ํ์ง ๋๊ตฌ
์ฝ๋ ์คํ์ผ๊ณผ ํ์ง์ ์๋์ผ๋ก ์ฒดํฌํ๋ ๋๊ตฌ๋ค์ ์ฌ์ฉํ์:
# PHP CodeSniffer ์ค์น
composer require --dev drupal/coder
# Drupal ์ฝ๋ฉ ํ์ค ๋ฑ๋ก
./vendor/bin/phpcs --config-set installed_paths vendor/drupal/coder/coder_sniffer
# ์ฝ๋ ์คํ์ผ ์ฒดํฌ
./vendor/bin/phpcs --standard=Drupal modules/custom/custom_greeting/
# ์๋ ์์
./vendor/bin/phpcbf --standard=Drupal modules/custom/custom_greeting/
# PHPStan์ผ๋ก ์ ์ ๋ถ์
composer require --dev phpstan/phpstan
./vendor/bin/phpstan analyse modules/custom/custom_greeting/
CI/CD ํ์ดํ๋ผ์ธ์ ์ด๋ฐ ๋๊ตฌ๋ค์ ํตํฉํ๋ฉด ์ฝ๋ ํ์ง์ ์๋์ผ๋ก ๊ด๋ฆฌํ ์ ์์ด! ๐ค
๐จ ํ ๋ง์ ํ๋ก ํธ์๋ ์ปค์คํฐ๋ง์ด์ง
๋ฐฑ์๋๋ง ์ ๋ง๋ค๋ฉด ๋ญํด? ์ฌ์ฉ์๊ฐ ๋ณด๋ ๊ฑด ํ๋ก ํธ์๋์ผ! Drupal์ ํ ๋ง ์์คํ ์ ๋ง์คํฐํด๋ณด์. ๐ญ
๐๏ธ ์ปค์คํ ํ ๋ง ๋ง๋ค๊ธฐ
Drupal 10๋ถํฐ๋ Twig ํ ํ๋ฆฟ ์์ง์ ์ฌ์ฉํด. PHP ์ฝ๋๋ฅผ ํ ํ๋ฆฟ์์ ๋ถ๋ฆฌํ ์ ์์ด์ ํจ์ฌ ๊น๋ํ์ง!
# mytheme.info.yml
name: My Custom Theme
type: theme
description: '์ฐ๋ฆฌ ํ์ฌ ์ ์ฉ ์ปค์คํ
ํ
๋ง'
core_version_requirement: ^9 || ^10
base theme: stable9
regions:
header: Header
primary_menu: 'Primary menu'
secondary_menu: 'Secondary menu'
breadcrumb: Breadcrumb
content: Content
sidebar_first: 'Left sidebar'
sidebar_second: 'Right sidebar'
footer: Footer
libraries:
- mytheme/global-styling
- mytheme/global-scripts
# ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ ์
# mytheme.libraries.yml
global-styling:
css:
theme:
css/style.css: {}
css/responsive.css: {}
js:
js/script.js: {}
dependencies:
- core/jquery
- core/drupal
- core/drupalSettings
Twig ํ ํ๋ฆฟ ์์:
{# templates/node--article.html.twig #}
{#
/**
* @file
* ๊ธฐ์ฌ ๋
ธ๋ ํ
ํ๋ฆฟ
*/
#}
<article{{ attributes.addClass('article-node') }}>
{% if label %}
<header class="article-header">
<h1{{ title_attributes.addClass('article-title') }}>
{{ label }}
</h1>
{% if display_submitted %}
<div class="article-meta">
<span class="author">{{ author_name }}</span>
<time datetime="{{ date|date('c') }}">
{{ date|date('Y-m-d') }}
</time>
</div>
{% endif %}
</header>
{% endif %}
<div{{ content_attributes.addClass('article-content') }}>
{{ content|without('links', 'comment') }}
</div>
{% if content.links %}
<footer class="article-footer">
{{ content.links }}
</footer>
{% endif %}
{% if content.comment %}
<section class="article-comments">
{{ content.comment }}
</section>
{% endif %}
</article>
Twig๋ ์ ๋ง ๊ฐ๋ ฅํด! ํํฐ, ํจ์, ์กฐ๊ฑด๋ฌธ, ๋ฐ๋ณต๋ฌธ ๋ค ์ฌ์ฉํ ์ ์์ด:
{# ๋ณ์ ์ถ๋ ฅ #}
{{ variable }}
{# ํํฐ ์ฌ์ฉ #}
{{ text|upper }}
{{ text|lower }}
{{ text|capitalize }}
{{ text|length }}
{{ date|date('Y-m-d') }}
{# ์กฐ๊ฑด๋ฌธ #}
{% if user.isAuthenticated %}
<p>ํ์ํฉ๋๋ค, {{ user.name }}๋!</p>
{% else %}
<p>๋ก๊ทธ์ธํด์ฃผ์ธ์.</p>
{% endif %}
{# ๋ฐ๋ณต๋ฌธ #}
{% for item in items %}
<div class="item">{{ item.title }}</div>
{% endfor %}
{# ๋ฐฐ์ด ์ ๊ทผ #}
{{ content.field_image }}
{{ content['field_image'] }}
{# ์ฃผ์ #}
{# ์ด๊ฒ์ ์ฃผ์์
๋๋ค #}
๐ฏ Preprocess ํจ์
ํ ํ๋ฆฟ์ ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํ๊ธฐ ์ ์ ๊ฐ๊ณตํ๊ณ ์ถ๋ค๋ฉด? Preprocess ํจ์๋ฅผ ์ฌ์ฉํด!
<?php
/**
* Implements template_preprocess_node().
*/
function mytheme_preprocess_node(&$variables) {
$node = $variables['node'];
// ์ฝ๊ธฐ ์๊ฐ ๊ณ์ฐ
$content = strip_tags($node->body->value);
$word_count = str_word_count($content);
$reading_time = ceil($word_count / 200); // ๋ถ๋น 200๋จ์ด ๊ฐ์
$variables['reading_time'] = $reading_time;
// ์์ฑ์ ์ ๋ณด ์ถ๊ฐ
$author = $node->getOwner();
$variables['author_name'] = $author->getDisplayName();
$variables['author_picture'] = $author->user_picture->entity->uri->value ?? NULL;
// ์ปค์คํ
ํด๋์ค ์ถ๊ฐ
$variables['attributes']['class'][] = 'node-' . $node->bundle();
$variables['attributes']['class'][] = 'node-' . $node->id();
if ($node->isPromoted()) {
$variables['attributes']['class'][] = 'node-promoted';
}
}
/**
* Implements template_preprocess_page().
*/
function mytheme_preprocess_page(&$variables) {
// ํ์ฌ ๊ฒฝ๋ก ์ ๋ณด
$current_path = \Drupal::service('path.current')->getPath();
$variables['current_path'] = $current_path;
// ์ฌ์ดํธ ์ ๋ณด
$config = \Drupal::config('system.site');
$variables['site_slogan'] = $config->get('slogan');
// ์ปค์คํ
๋ณ์
$variables['year'] = date('Y');
$variables['company_name'] = '์ฐ๋ฆฌ ํ์ฌ';
}
์ด์ ํ ํ๋ฆฟ์์ ์ด ๋ณ์๋ค์ ์ฌ์ฉํ ์ ์์ด:
{# templates/node--article.html.twig #}
<article{{ attributes }}>
<header>
<h1>{{ label }}</h1>
<div class="meta">
<span class="author">{{ author_name }}</span>
<span class="reading-time">๐ {{ reading_time }}๋ถ ์์</span>
</div>
</header>
{{ content }}
</article>
๐จ CSS์ JavaScript ๊ด๋ฆฌ
Drupal์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์์คํ ์ผ๋ก CSS/JS๋ฅผ ๊ด๋ฆฌํด. ํ์ํ ํ์ด์ง์๋ง ๋ก๋ํ ์ ์์ด์ ์ฑ๋ฅ์ด ์ข์!
<?php
/**
* ํน์ ํ์ด์ง์๋ง ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ถ๊ฐ
*/
function mytheme_preprocess_node(&$variables) {
$node = $variables['node'];
// ๊ธฐ์ฌ ํ์
์๋ง ํน๋ณํ ์คํ์ผ ์ ์ฉ
if ($node->bundle() == 'article') {
$variables['#attached']['library'][] = 'mytheme/article-styling';
}
// ๋์์์ด ์์ผ๋ฉด ๋น๋์ค ํ๋ ์ด์ด ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ถ๊ฐ
if (!$node->field_video->isEmpty()) {
$variables['#attached']['library'][] = 'mytheme/video-player';
}
}
/**
* JavaScript์ ์ค์ ์ ๋ฌ
*/
function mytheme_preprocess_page(&$variables) {
$variables['#attached']['drupalSettings']['mytheme'] = [
'apiEndpoint' => 'https://api.example.com',
'userId' => \Drupal::currentUser()->id(),
'language' => \Drupal::languageManager()->getCurrentLanguage()->getId(),
];
}
JavaScript์์ ์ด ์ค์ ์ ์ฌ์ฉํ๊ธฐ:
// js/script.js
(function ($, Drupal, drupalSettings) {
'use strict';
Drupal.behaviors.myThemeBehavior = {
attach: function (context, settings) {
// Drupal ์ค์ ์ฌ์ฉ
const apiEndpoint = drupalSettings.mytheme.apiEndpoint;
const userId = drupalSettings.mytheme.userId;
// ํ ๋ฒ๋ง ์คํ๋๋๋ก context ์ฌ์ฉ
$('.my-element', context).once('myThemeBehavior').each(function () {
// ์ด ์ฝ๋๋ ๊ฐ ์์์ ํ ๋ฒ๋ง ์คํ๋จ
$(this).on('click', function () {
console.log('Clicked!');
});
});
}
};
})(jQuery, Drupal, drupalSettings);
๐ REST API์ ํค๋๋ฆฌ์ค CMS
์์ฆ์ ๋ชจ๋ฐ์ผ ์ฑ, SPA(Single Page Application), IoT ๊ธฐ๊ธฐ ๋ฑ ๋ค์ํ ํด๋ผ์ด์ธํธ๊ฐ ์์์? Drupal์ ํค๋๋ฆฌ์ค CMS๋ก ์ฌ์ฉํ๋ฉด ์ด ๋ชจ๋ ๊ฑธ ์ง์ํ ์ ์์ด! ๐
๐ REST API ํ์ฑํ
Drupal์ ๊ธฐ๋ณธ์ ์ผ๋ก REST API๋ฅผ ์ ๊ณตํด. ํ์ฑํ๋ง ํ๋ฉด ๋ผ:
# ํ์ํ ๋ชจ๋ ์ค์น
drush en rest restui serialization basic_auth -y
# ๋๋ Composer๋ก
composer require drupal/restui
REST ๋ฆฌ์์ค ์ค์ ํ๊ธฐ:
# config/sync/rest.resource.entity.node.yml
langcode: en
status: true
dependencies:
module:
- basic_auth
- node
- serialization
id: entity:node
plugin_id: 'entity:node'
granularity: resource
configuration:
methods:
- GET
- POST
- PATCH
- DELETE
formats:
- json
- xml
authentication:
- basic_auth
- cookie
์ด์ API๋ฅผ ์ฌ์ฉํ ์ ์์ด!
# ๋
ธ๋ ๋ชฉ๋ก ์กฐํ
GET /node?_format=json
# ํน์ ๋
ธ๋ ์กฐํ
GET /node/1?_format=json
# ๋
ธ๋ ์์ฑ
POST /node?_format=json
Content-Type: application/json
Authorization: Basic base64(username:password)
{
"type": [{"target_id": "article"}],
"title": [{"value": "์ ๊ธฐ์ฌ"}],
"body": [{"value": "๊ธฐ์ฌ ๋ด์ฉ"}]
}
# ๋
ธ๋ ์์
PATCH /node/1?_format=json
Content-Type: application/json
{
"title": [{"value": "์์ ๋ ์ ๋ชฉ"}]
}
# ๋
ธ๋ ์ญ์
DELETE /node/1?_format=json
๐ฏ ์ปค์คํ REST ๋ฆฌ์์ค
๊ธฐ๋ณธ REST API๋ก ๋ถ์กฑํ๋ค๋ฉด? ์ปค์คํ ๋ฆฌ์์ค๋ฅผ ๋ง๋ค์!
<?php
namespace Drupal\custom_greeting\Plugin\rest\resource;
use Drupal\rest\Plugin\ResourceBase;
use Drupal\rest\ResourceResponse;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
* ์ธ์ฌ๋ง REST ๋ฆฌ์์ค
*
* @RestResource(
* id = "greeting_resource",
* label = @Translation("Greeting Resource"),
* uri_paths = {
* "canonical" = "/api/greeting/{name}",
* "create" = "/api/greeting"
* }
* )
*/
class GreetingResource extends ResourceBase {
/**
* GET ์์ฒญ ์ฒ๋ฆฌ
*
* @param string $name
* ์ฌ์ฉ์ ์ด๋ฆ
*
* @return \Drupal\rest\ResourceResponse
* ์๋ต
*/
public function get($name = NULL) {
if (empty($name)) {
throw new BadRequestHttpException('Name parameter is required');
}
$greeting_service = \Drupal::service('custom_greeting.greeting_service');
$message = $greeting_service->createGreeting($name);
$response = [
'message' => $message,
'timestamp' => time(),
'name' => $name,
];
return new ResourceResponse($response);
}
/**
* POST ์์ฒญ ์ฒ๋ฆฌ
*
* @param array $data
* ์์ฒญ ๋ฐ์ดํฐ
*
* @return \Drupal\rest\ResourceResponse
* ์๋ต
*/
public function post(array $data) {
if (empty($data['name'])) {
throw new BadRequestHttpException('Name is required');
}
// ์ธ์ฌ๋ง ๋ก๊ทธ ์ ์ฅ
$connection = \Drupal::database();
$connection->insert('custom_greeting_logs')
->fields([
'uid' => \Drupal::currentUser()->id(),
'message' => $data['message'] ?? '',
'created' => time(),
])
->execute();
$response = [
'status' => 'success',
'message' => 'Greeting logged successfully',
];
return new ResourceResponse($response, 201);
}
}
๐ API ์ธ์ฆ
API ๋ณด์์ ์ ๋ง ์ค์ํด! ์ฌ๋ฌ ์ธ์ฆ ๋ฐฉ๋ฒ์ ์ฌ์ฉํ ์ ์์ด:
2. Cookie: ์น ๋ธ๋ผ์ฐ์ ์์ ์ฌ์ฉ
3. OAuth 2.0: ๊ฐ์ฅ ์์ ํ๊ณ ํ์ค์ ์ธ ๋ฐฉ๋ฒ
4. JWT: ํ ํฐ ๊ธฐ๋ฐ ์ธ์ฆ
OAuth 2.0 ์ค์ ํ๊ธฐ:
# Simple OAuth ๋ชจ๋ ์ค์น
composer require drupal/simple_oauth
drush en simple_oauth -y
# ํค ์์ฑ
mkdir -p /path/to/keys
openssl genrsa -out /path/to/keys/private.key 2048
openssl rsa -in /path/to/keys/private.key -pubout > /path/to/keys/public.key
# ์ค์ ์์ ํค ๊ฒฝ๋ก ์ง์
# /admin/config/people/simple_oauth
ํด๋ผ์ด์ธํธ์์ ํ ํฐ ๋ฐ๊ธฐ:
# ํ ํฐ ์์ฒญ
POST /oauth/token
Content-Type: application/x-www-form-urlencoded
grant_type=password&
client_id=YOUR_CLIENT_ID&
client_secret=YOUR_CLIENT_SECRET&
username=admin&
password=password
# ์๋ต
{
"token_type": "Bearer",
"expires_in": 300,
"access_token": "eyJ0eXAiOiJKV1QiLCJhbGc...",
"refresh_token": "def50200a8d9c7..."
}
# API ์์ฒญ ์ ํ ํฐ ์ฌ์ฉ
GET /api/greeting/John
Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGc...
๐ Views์ ๋ฐ์ดํฐ ๊ด๋ฆฌ
Views๋ Drupal์ ํฌ๋ฌ ๊ธฐ๋ฅ ์ค ํ๋์ผ! SQL ์ฟผ๋ฆฌ ์์ด๋ ๋ณต์กํ ๋ฐ์ดํฐ ๋ชฉ๋ก์ ๋ง๋ค ์ ์๊ฑฐ๋ . ํ์ง๋ง ํ๋ก๊ทธ๋๋จธ๋ผ๋ฉด ์ฝ๋๋ก๋ ๋ค๋ฃฐ ์ค ์์์ผ์ง! ๐ช
๐ฏ ํ๋ก๊ทธ๋๋ฐ ๋ฐฉ์์ผ๋ก Views ์์ ํ๊ธฐ
<?php
/**
* Implements hook_views_pre_view().
*
* View๊ฐ ์คํ๋๊ธฐ ์ ์ ์์
*/
function custom_greeting_views_pre_view(\Drupal\views\ViewExecutable $view, $display_id, array &$args) {
// ํน์ View๋ง ์์
if ($view->id() == 'content' && $display_id == 'page_1') {
// ํ์ฌ ์ฌ์ฉ์๊ฐ ์์ฑํ ์ฝํ
์ธ ๋ง ํ์
$current_user = \Drupal::currentUser();
if (!$current_user->hasPermission('view all content')) {
$view->setArguments([$current_user->id()]);
}
}
}
/**
* Implements hook_views_query_alter().
*
* View์ ์ฟผ๋ฆฌ ์์
*/
function custom_greeting_views_query_alter(\Drupal\views\ViewExecutable $view, \Drupal\views\Plugin\views\query\QueryPluginBase $query) {
if ($view->id() == 'articles') {
// ํน์ ์กฐ๊ฑด ์ถ๊ฐ
$query->addWhere(0, 'node_field_data.status', 1);
$query->addWhere(0, 'node_field_data.created', strtotime('-30 days'), '>');
// ์ ๋ ฌ ๋ณ๊ฒฝ
$query->orderby = [];
$query->addOrderBy('node_field_data', 'created', 'DESC');
}
}
/**
* Implements hook_views_pre_render().
*
* ๋ ๋๋ง ์ ์ ๊ฒฐ๊ณผ ์์
*/
function custom_greeting_views_pre_render(\Drupal\views\ViewExecutable $view) {
if ($view->id() == 'user_list') {
foreach ($view->result as $row) {
// ๊ฐ ํ์ ์ปค์คํ
๋ฐ์ดํฐ ์ถ๊ฐ
$user = $row->_entity;
$row->custom_data = [
'login_count' => getLoginCount($user->id()),
'last_login' => $user->getLastLoginTime(),
];
}
}
}
๐ง ํ๋ก๊ทธ๋๋ฐ ๋ฐฉ์์ผ๋ก View ์์ฑํ๊ธฐ
GUI๋ก ๋ง๋ค๊ธฐ ๋ณต์กํ View๋ ์ฝ๋๋ก ๋ง๋ค ์๋ ์์ด:
<?php
use Drupal\views\Entity\View;
/**
* ํ๋ก๊ทธ๋๋ฐ ๋ฐฉ์์ผ๋ก View ์์ฑ
*/
function createCustomView() {
$view = View::create([
'id' => 'custom_articles',
'label' => 'Custom Articles',
'module' => 'custom_greeting',
'description' => '์ปค์คํ
๊ธฐ์ฌ ๋ชฉ๋ก',
'tag' => 'custom',
'base_table' => 'node_field_data',
'display' => [
'default' => [
'display_plugin' => 'default',
'id' => 'default',
'display_title' => 'Master',
'position' => 0,
'display_options' => [
'access' => [
'type' => 'perm',
'options' => [
'perm' => 'access content',
],
],
'cache' => [
'type' => 'tag',
],
'query' => [
'type' => 'views_query',
],
'exposed_form' => [
'type' => 'basic',
],
'pager' => [
'type' => 'full',
'options' => [
'items_per_page' => 10,
],
],
'style' => [
'type' => 'default',
],
'row' => [
'type' => 'fields',
],
'fields' => [
'title' => [
'id' => 'title',
'table' => 'node_field_data',
'field' => 'title',
'label' => '์ ๋ชฉ',
],
'created' => [
'id' => 'created',
'table' => 'node_field_data',
'field' => 'created',
'label' => '์์ฑ์ผ',
],
],
'filters' => [
'status' => [
'id' => 'status',
'table' => 'node_field_data',
'field' => 'status',
'value' => 1,
],
'type' => [
'id' => 'type',
'table' => 'node_field_data',
'field' => 'type',
'value' => [
'article' => 'article',
],
],
],
'sorts' => [
'created' => [
'id' => 'created',
'table' => 'node_field_data',
'field' => 'created',
'order' => 'DESC',
],
],
],
],
'page_1' => [
'display_plugin' => 'page',
'id' => 'page_1',
'display_title' => 'Page',
'position' => 1,
'display_options' => [
'path' => 'custom-articles',
],
],
],
]);
$view->save();
}
๋ณต์กํด ๋ณด์ด์ง๋ง, ํ ๋ฒ ๋ง๋ค์ด๋๋ฉด ์ฌ์ฌ์ฉํ๊ธฐ ํธํด! ํนํ ๋ฉํฐ์ฌ์ดํธ ํ๊ฒฝ์์ ์ ์ฉํ์ง. ๐ฏ
๐ ๋ค๊ตญ์ด์ ๋ฒ์ญ ๊ด๋ฆฌ
๊ธ๋ก๋ฒ ์๋น์ค๋ฅผ ๋ง๋ ๋ค๋ฉด ๋ค๊ตญ์ด ์ง์์ ํ์์ผ! Drupal์ ๋ค๊ตญ์ด ์ง์์ด ์ ๋ง ๊ฐ๋ ฅํด. ๐ฌ
๐ฃ๏ธ ๋ค๊ตญ์ด ์ค์
# ํ์ํ ๋ชจ๋ ์ค์น
drush en language locale content_translation config_translation -y
# ์ธ์ด ์ถ๊ฐ (ํ๊ตญ์ด)
drush language-add ko
# ๊ธฐ๋ณธ ์ธ์ด ์ค์
drush config-set system.site default_langcode ko -y
์ฝ๋์์ ๋ฒ์ญ ๊ฐ๋ฅํ ๋ฌธ์์ด ๋ง๋ค๊ธฐ:
<?php
// ๊ธฐ๋ณธ ๋ฒ์ญ
$text = t('Hello, World!');
// ํ๋ ์ด์คํ๋ ์ฌ์ฉ
$text = t('Hello, @name!', ['@name' => $name]);
// ๋ณต์ํ ์ฒ๋ฆฌ
$text = \Drupal::translation()->formatPlural(
$count,
'1 item',
'@count items'
);
// ์ปจํ
์คํธ ์ง์ (๊ฐ์ ๋จ์ด๋ ๋ฌธ๋งฅ์ ๋ฐ๋ผ ๋ค๋ฅด๊ฒ ๋ฒ์ญ)
$text = t('Post', [], ['context' => 'verb']); // ๊ฒ์ํ๋ค
$text = t('Post', [], ['context' => 'noun']); // ๊ฒ์๋ฌผ
// JavaScript๋ก ์ ๋ฌ
$build['#attached']['drupalSettings']['myModule']['message'] = t('Success!');
// Twig์์ ๋ฒ์ญ
// {{ 'Hello, World!'|t }}
// {{ 'Hello, @name!'|t({'@name': name}) }}
๐ ์ฝํ ์ธ ๋ฒ์ญ
๋ ธ๋, ํ์๋ ธ๋ฏธ, ๋ธ๋ก ๋ฑ ๋ชจ๋ ์ํฐํฐ๋ฅผ ๋ฒ์ญํ ์ ์์ด:
<?php
use Drupal\node\Entity\Node;
// ์์ด ๋
ธ๋ ์์ฑ
$node = Node::create([
'type' => 'article',
'title' => 'Hello, World!',
'body' => 'This is an English article.',
'langcode' => 'en',
]);
$node->save();
// ํ๊ตญ์ด ๋ฒ์ญ ์ถ๊ฐ
if ($node->isTranslatable()) {
$node_ko = $node->addTranslation('ko', [
'title' => '์๋
ํ์ธ์, ์ธ๊ณ!',
'body' => '์ด๊ฒ์ ํ๊ตญ์ด ๊ธฐ์ฌ์
๋๋ค.',
]);
$node_ko->save();
}
// ํ์ฌ ์ธ์ด๋ก ๋
ธ๋ ๋ก๋
$node = Node::load(1);
$translated = \Drupal::service('entity.repository')
->getTranslationFromContext($node);
echo $translated->getTitle(); // ํ์ฌ ์ธ์ด์ ๋ง๋ ์ ๋ชฉ
// ํน์ ์ธ์ด๋ก ๋ก๋
$node_ko = $node->getTranslation('ko');
echo $node_ko->getTitle(); // ํ๊ตญ์ด ์ ๋ชฉ
๐ ๋ฐฐํฌ์ DevOps
๊ฐ๋ฐ์ด ๋๋ฌ๋ค๊ณ ๋์ด ์๋์ผ! ์์ ํ๊ณ ํจ์จ์ ์ธ ๋ฐฐํฌ๊ฐ ์ค์ํ์ง. ์ํฐํ๋ผ์ด์ฆ ํ๊ฒฝ์์์ ๋ฐฐํฌ ์ ๋ต์ ์์๋ณด์! ๐ฏ
๐ฆ Configuration Management
Drupal 8๋ถํฐ๋ ์ค์ ์ YAML ํ์ผ๋ก ๊ด๋ฆฌํ ์ ์์ด. ์ด๊ฒ ์ ๋ง ํ๋ช ์ ์ด์ผ!
# ํ์ฌ ์ค์ ์ ํ์ผ๋ก ๋ด๋ณด๋ด๊ธฐ
drush config-export
# ํ์ผ์ ์ค์ ์ ์ฌ์ดํธ์ ์ ์ฉ
drush config-import
# ํน์ ์ค์ ๋ง ๋ด๋ณด๋ด๊ธฐ
drush config-get system.site
# ํน์ ์ค์ ๋ณ๊ฒฝ
drush config-set system.site name "My New Site Name"
# ์ค์ ์ฐจ์ด ํ์ธ
drush config-status
์ด๋ ๊ฒ ํ๋ฉด Git์ผ๋ก ์ค์ ์ ๋ฒ์ ๊ด๋ฆฌํ ์ ์์ด! ๊ฐ๋ฐ, ์คํ ์ด์ง, ํ๋ก๋์ ํ๊ฒฝ ๊ฐ ์ค์ ๋๊ธฐํ๊ฐ ์ ๋ง ์ฌ์์ง์ง. ๐
๐ CI/CD ํ์ดํ๋ผ์ธ
GitLab CI ์์๋ฅผ ๋ณด์ฌ์ค๊ฒ:
# .gitlab-ci.yml
stages:
- test
- build
- deploy
variables:
MYSQL_DATABASE: drupal
MYSQL_ROOT_PASSWORD: root
# ์ฝ๋ ํ์ง ๊ฒ์ฌ
code_quality:
stage: test
image: php:8.1
script:
- composer install
- ./vendor/bin/phpcs --standard=Drupal modules/custom/
- ./vendor/bin/phpstan analyse modules/custom/
only:
- merge_requests
# ๋จ์ ํ
์คํธ
unit_tests:
stage: test
image: php:8.1
services:
- mysql:8.0
script:
- composer install
- ./vendor/bin/phpunit modules/custom/
only:
- merge_requests
# ๋น๋
build:
stage: build
image: php:8.1
script:
- composer install --no-dev --optimize-autoloader
- drush cr
artifacts:
paths:
- vendor/
- web/
only:
- main
# ์คํ
์ด์ง ๋ฐฐํฌ
deploy_staging:
stage: deploy
script:
- rsync -avz --delete ./ user@staging-server:/var/www/drupal/
- ssh user@staging-server "cd /var/www/drupal && drush updb -y && drush cr"
environment:
name: staging
url: https://staging.example.com
only:
- main
# ํ๋ก๋์
๋ฐฐํฌ
deploy_production:
stage: deploy
script:
- rsync -avz --delete ./ user@prod-server:/var/www/drupal/
- ssh user@prod-server "cd /var/www/drupal && drush updb -y && drush cr"
environment:
name: production
url: https://www.example.com
when: manual
only:
- main
๐ณ Docker ์ปจํ ์ด๋ํ
ํ๋ก๋์ ์ฉ Dockerfile:
# Dockerfile
FROM php:8.1-apache
# ํ์ํ PHP ํ์ฅ ์ค์น
RUN apt-get update && apt-get install -y \
libpng-dev \
libjpeg-dev \
libpq-dev \
libzip-dev \
git \
unzip \
&& docker-php-ext-configure gd --with-jpeg \
&& docker-php-ext-install -j$(nproc) gd pdo pdo_mysql zip opcache
# Composer ์ค์น
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
# Apache ์ค์
RUN a2enmod rewrite
COPY apache-config.conf /etc/apache2/sites-available/000-default.conf
# Drupal ํ์ผ ๋ณต์ฌ
WORKDIR /var/www/html
COPY . .
# ์์กด์ฑ ์ค์น
RUN composer install --no-dev --optimize-autoloader
# ๊ถํ ์ค์
RUN chown -R www-data:www-data /var/www/html
# ํฌ์ค์ฒดํฌ
HEALTHCHECK --interval=30s --timeout=3s \
CMD curl -f http://localhost/ || exit 1
EXPOSE 80
Kubernetes ๋ฐฐํฌ ์ค์ :
# kubernetes/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: drupal
spec:
replicas: 3
selector:
matchLabels:
app: drupal
template:
metadata:
labels:
app: drupal
spec:
containers:
- name: drupal
image: myregistry/drupal:latest
ports:
- containerPort: 80
env:
- name: DRUPAL_DATABASE_HOST
value: mysql-service
- name: DRUPAL_DATABASE_NAME
valueFrom:
secretKeyRef:
name: drupal-secrets
key: database-name
resources:
requests:
memory: "512Mi"
cpu: "500m"
limits:
memory: "1Gi"
cpu: "1000m"
livenessProbe:
httpGet:
path: /
port: 80
initialDelaySeconds: 30
periodSeconds: 10
readinessProbe:
httpGet:
path: /
port: 80
initialDelaySeconds: 5
periodSeconds: 5
โข ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ ๋ฐ์ดํธ๋ ์ ์คํ๊ฒ
โข ์บ์ ํด๋ฆฌ์ด๋ฅผ ์์ง ๋ง ๊ฒ
โข ๋กค๋ฐฑ ๊ณํ์ ์ค๋นํ ๊ฒ
โข ๋ชจ๋ํฐ๋ง ์์คํ ํ์ธ
๐ ๋ชจ๋ํฐ๋ง๊ณผ ๋ก๊น
์ฌ์ดํธ๊ฐ ์ ๋์๊ฐ๋์ง ์ด๋ป๊ฒ ์ ์ ์์๊น? ๋ชจ๋ํฐ๋ง๊ณผ ๋ก๊น ์ด ๋ต์ด์ผ! ๐
๐ ์ฑ๋ฅ ๋ชจ๋ํฐ๋ง
<?php
/**
* ์ปค์คํ
๋ก๊น
*/
\Drupal::logger('custom_greeting')->notice('์ฌ์ฉ์ @user๊ฐ ๋ก๊ทธ์ธํ์ต๋๋ค.', [
'@user' => $user->getDisplayName(),
]);
\Drupal::logger('custom_greeting')->error('API ํธ์ถ ์คํจ: @error', [
'@error' => $exception->getMessage(),
]);
\Drupal::logger('custom_greeting')->warning('์บ์ ๋ฏธ์ค์จ์ด ๋์ต๋๋ค: @rate%', [
'@rate' => $cache_miss_rate,
]);
/**
* ์ฑ๋ฅ ์ธก์
*/
$start_time = microtime(TRUE);
// ๋ฌด๊ฑฐ์ด ์์
performExpensiveOperation();
$end_time = microtime(TRUE);
$execution_time = ($end_time - $start_time) * 1000; // ๋ฐ๋ฆฌ์ด
if ($execution_time > 1000) {
\Drupal::logger('performance')->warning('๋๋ฆฐ ์์
๊ฐ์ง: @time ms', [
'@time' => $execution_time,
]);
}
๐ ์๋ฆผ ์ค์
New Relic, Datadog ๊ฐ์ APM ๋๊ตฌ๋ฅผ ์ฐ๋ํ๋ฉด ์ค์๊ฐ ๋ชจ๋ํฐ๋ง์ด ๊ฐ๋ฅํด:
# New Relic PHP ์์ด์ ํธ ์ค์น
apt-get install newrelic-php5
newrelic-install install
# php.ini ์ค์
newrelic.appname = "My Drupal Site"
newrelic.license = "YOUR_LICENSE_KEY"
newrelic.enabled = true
Sentry๋ก ์๋ฌ ์ถ์ ํ๊ธฐ:
# Sentry ๋ชจ๋ ์ค์น
composer require drupal/raven
drush en raven -y
# settings.php์ ์ค์ ์ถ๊ฐ
$config['raven.settings']['client_key'] = 'YOUR_SENTRY_DSN';
$config['raven.settings']['environment'] = 'production';
๐ ์ค์ ํ๋ก์ ํธ ์ฌ๋ก
์ด๋ก ๋ง ์๋ฉด ๋ญํด? ์ค์ ๊ฒฝํ์ด ์ค์ํ์ง! ์ค์ ์ํฐํ๋ผ์ด์ฆ ํ๋ก์ ํธ ์ฌ๋ก๋ฅผ ๊ณต์ ํ ๊ฒ. ๐ผ
๐ข ๋๊ธฐ์ ์ธํธ๋ผ๋ท ๊ตฌ์ถ
5๋ง ๋ช ์ ์ง์์ด ์ฌ์ฉํ๋ ์ธํธ๋ผ๋ท์ Drupal๋ก ๊ตฌ์ถํ ๊ฒฝํ์ด์ผ:
โข ๋ถ์๋ณ ๊ฒ์ํ
โข ๋ฌธ์ ๊ด๋ฆฌ ์์คํ
โข ์ํฌํ๋ก์ฐ ์น์ธ ์์คํ
โข ๋ค๊ตญ์ด ์ง์ (ํ/์/์ค/์ผ)
โข SSO(Single Sign-On) ์ฐ๋
โข ๋ชจ๋ฐ์ผ ์ฑ API
ํต์ฌ ์ํคํ ์ฒ:
<?php
/**
* LDAP ์ธ์ฆ ํตํฉ
*/
function custom_intranet_user_login($account) {
// LDAP์์ ์ฌ์ฉ์ ์ ๋ณด ๋๊ธฐํ
$ldap_service = \Drupal::service('ldap.servers');
$ldap_user = $ldap_service->getUserFromLdap($account->getAccountName());
if ($ldap_user) {
// ๋ถ์ ์ ๋ณด ์
๋ฐ์ดํธ
$account->set('field_department', $ldap_user['department']);
$account->set('field_position', $ldap_user['title']);
$account->set('field_phone', $ldap_user['phone']);
$account->save();
}
}
/**
* ์ํฌํ๋ก์ฐ ์น์ธ ์์คํ
*/
class ApprovalWorkflow {
public function submitForApproval($node) {
// ์น์ธ ์์ฒญ ์์ฑ
$approval = ApprovalEntity::create([
'node_id' => $node->id(),
'requester' => \Drupal::currentUser()->id(),
'status' => 'pending',
'created' => time(),
]);
// ์น์ธ์์๊ฒ ์๋ฆผ
$approvers = $this->getApprovers($node);
foreach ($approvers as $approver) {
$this->sendNotification($approver, $node);
}
$approval->save();
}
public function approve($approval_id) {
$approval = ApprovalEntity::load($approval_id);
$approval->set('status', 'approved');
$approval->set('approved_by', \Drupal::currentUser()->id());
$approval->set('approved_at', time());
$approval->save();
// ์๋ณธ ๋
ธ๋ ๋ฐํ
$node = Node::load($approval->get('node_id')->value);
$node->setPublished();
$node->save();
}
}
์ฑ๋ฅ ์ต์ ํ ๊ฒฐ๊ณผ:
| ์งํ | ์ต์ ํ ์ | ์ต์ ํ ํ |
|---|---|---|
| ํ๊ท ํ์ด์ง ๋ก๋ | 3.2์ด | 0.8์ด |
| ๋์ ์ ์์ | 500๋ช | 5,000๋ช |
| ์๋ฒ ๋์ | 8๋ | 4๋ |
| ์ ๋น์ฉ | $10,000 | $5,000 |
๐ ๊ธ๋ก๋ฒ ์ด์ปค๋จธ์ค ํ๋ซํผ
Drupal Commerce๋ฅผ ์ฌ์ฉํ ๋ค๊ตญ๊ฐ ์ด์ปค๋จธ์ค ์ฌ์ดํธ:
<?php
/**
* ๋ค์ค ํตํ ์ง์
*/
function custom_commerce_get_price($product, $currency = NULL) {
if (!$currency) {
$currency = \Drupal::service('commerce_currency_resolver.current_currency')->getCurrency();
}
$price = $product->getPrice();
// ํ์จ ์ ์ฉ
$exchange_rate = \Drupal::service('commerce_exchanger.exchange_rates')
->load($price->getCurrencyCode(), $currency);
$converted_price = new Price(
$price->getNumber() * $exchange_rate,
$currency
);
return $converted_price;
}
/**
* ์ฌ๊ณ ๊ด๋ฆฌ
*/
function custom_commerce_check_stock($product_variation) {
$stock_level = $product_variation->get('field_stock_level')->value;
if ($stock_level < 10) {
// ์ฌ๊ณ ๋ถ์กฑ ์๋ฆผ
\Drupal::service('custom_commerce.stock_alert')
->sendLowStockAlert($product_variation);
}
return $stock_level > 0;
}
๐ก ์ฌ๋ฅ๋ท์์ Drupal ์ ๋ฌธ๊ฐ ์ฐพ๊ธฐ
Drupal ๊ฐ๋ฐ์ด ์ฒ์์ด๋ผ ๋ง๋งํ๋ค๊ณ ? ๊ฑฑ์ ๋ง! ์ฌ๋ฅ๋ท์๋ Drupal ์ ๋ฌธ๊ฐ๋ค์ด ๋ง์. ๐ฏ
์ฌ๋ฅ๋ท(https://www.jaenung.net)์์๋ Drupal ๋ชจ๋ ๊ฐ๋ฐ, ํ ๋ง ์ปค์คํฐ๋ง์ด์ง, ์ฑ๋ฅ ์ต์ ํ, ๋ณด์ ์ปจ์คํ ๋ฑ ๋ค์ํ ์๋น์ค๋ฅผ ์ ๊ณตํ๋ ์ ๋ฌธ๊ฐ๋ค์ ๋ง๋ ์ ์์ด. ํ๋ก์ ํธ ๊ท๋ชจ์ ๋ง๋ ์ ์ ํ ์ ๋ฌธ๊ฐ๋ฅผ ์ฐพ์์ ํ์ ํ ์ ์์ง!
ํนํ ์ํฐํ๋ผ์ด์ฆ๊ธ ํ๋ก์ ํธ๋ ํผ์ ํ๊ธฐ ์ด๋ ค์ธ ์ ์์ด. ๊ฒฝํ ๋ง์ Drupal ๊ฐ๋ฐ์์ ๋์์ ๋ฐ์ผ๋ฉด ์ํ์ฐฉ์ค๋ฅผ ์ค์ด๊ณ ํ๋ก์ ํธ๋ฅผ ์ฑ๊ณต์ ์ผ๋ก ์๋ฃํ ์ ์์ด. ์ฌ๋ฅ๋ท์์ ๋น์ ์ ํ๋ก์ ํธ์ ๋ฑ ๋ง๋ ์ ๋ฌธ๊ฐ๋ฅผ ์ฐพ์๋ณด๋ ๊ฑด ์ด๋? ๐
๐ฏ ๋ง๋ฌด๋ฆฌํ๋ฉฐ
์! ์ ๋ง ๊ธด ์ฌ์ ์ด์์ง? ๐ Drupal ๋ชจ๋ ๊ฐ๋ฐ๋ถํฐ ์ํฐํ๋ผ์ด์ฆ๊ธ ์ปค์คํฐ๋ง์ด์ง๊น์ง ์ ๋ง ๋ง์ ๋ด์ฉ์ ๋ค๋ค์ด.
Drupal์ ํ์ต ๊ณก์ ์ด ๊ฐํ๋ฅธ ํธ์ด์ผ. ํ์ง๋ง ํ ๋ฒ ์ตํ๋ฉด ์ ๋ง ๊ฐ๋ ฅํ ๋๊ตฌ๊ฐ ๋์ง. ํนํ ๋๊ท๋ชจ ํ๋ก์ ํธ์์๋ Drupal๋ง ํ ๊ฒ ์์ด. ๐
- โ Drupal์ ๋ชจ๋ ๊ธฐ๋ฐ ์ํคํ ์ฒ๋ก ํ์ฅ์ฑ์ด ๋ฐ์ด๋จ
- โ Hook ์์คํ ์ผ๋ก ์ฝ์ด๋ฅผ ์์ ํ์ง ์๊ณ ๊ธฐ๋ฅ ํ์ฅ ๊ฐ๋ฅ
- โ ์๋น์ค ์ปจํ ์ด๋์ DI๋ก ๊น๋ํ ์ฝ๋ ์์ฑ
- โ ์บ์ฑ๊ณผ ์ฟผ๋ฆฌ ์ต์ ํ๋ก ์ฑ๋ฅ ํฅ์
- โ ๋ณด์์ ํํํ ์ ์๋ ์์ญ
- โ ํ ์คํธ ์ฝ๋ ์์ฑ์ ํ์
- โ REST API๋ก ํค๋๋ฆฌ์ค CMS ๊ตฌํ ๊ฐ๋ฅ
- โ Configuration Management๋ก ๋ฐฐํฌ ์๋ํ
- โ ๋ชจ๋ํฐ๋ง๊ณผ ๋ก๊น ์ผ๋ก ์์ ์ ์ธ ์ด์
Drupal ์ปค๋ฎค๋ํฐ๋ ์ ๋ง ํ๋ฐํด. ๋ฌธ์ ๊ฐ ์๊ธฐ๋ฉด Drupal.org ํฌ๋ผ์ด๋ Stack Overflow์์ ๋์์ ๋ฐ์ ์ ์์ด. ํ๊ตญ Drupal ์ฌ์ฉ์ ๊ทธ๋ฃน๋ ์์ผ๋ ์ฐธ์ฌํด๋ณด๋ ๊ฒ๋ ์ข์! ๐ค
๋ง์ง๋ง์ผ๋ก, Drupal ๊ฐ๋ฐ์ ๊ณ์ ์งํํ๊ณ ์์ด. Drupal 11๋ ๊ณง ๋์ฌ ์์ ์ด๊ณ , Symfony 7 ํตํฉ, PHP 8.3 ์ง์ ๋ฑ ์๋ก์ด ๊ธฐ๋ฅ๋ค์ด ์ถ๊ฐ๋ ๊ฑฐ์ผ. ๊ณ์ ํ์ตํ๊ณ ๋ฐ์ ํ๋ ์์ธ๊ฐ ์ค์ํด!
์ด ๊ธ์ด Drupal ๊ฐ๋ฐ ์ฌ์ ์ ๋์์ด ๋์์ผ๋ฉด ์ข๊ฒ ์ด. ๊ถ๊ธํ ์ ์ด ์์ผ๋ฉด ์ธ์ ๋ ๋ฌผ์ด๋ด! ํจ๊ป ์ฑ์ฅํ๋ ๊ฐ๋ฐ์๊ฐ ๋์! ๐ชโจ
Happy Drupal Coding! ๐๐
๊ด๋ จ ํค์๋
๋๊ธ ์์ฑ
์ด ๊ธ์ ๋ํ ์ฌ๋ฌ๋ถ์ ์๊ฐ์ ๋ค๋ ค์ฃผ์ธ์
๋ก๊ทธ์ธ์ด ํ์ํฉ๋๋ค
๋๊ธ์ ์์ฑํ๋ ค๋ฉด ๋จผ์ ๋ก๊ทธ์ธํด์ฃผ์ธ์.
๋๊ธ 0
์ง์์ธ์ ์ฒ - ์ง์ ์ฌ์ฐ๊ถ ๋ณดํธ ๊ณ ์ง
์ง์ ์ฌ์ฐ๊ถ ๋ณดํธ ๊ณ ์ง
- ์ ์๊ถ ๋ฐ ์์ ๊ถ: ๋ณธ ์ปจํ ์ธ ๋ ์ฌ๋ฅ๋ท์ ๋ ์ AI ๊ธฐ์ ๋ก ์์ฑ๋์์ผ๋ฉฐ, ๋ํ๋ฏผ๊ตญ ์ ์๊ถ๋ฒ ๋ฐ ๊ตญ์ ์ ์๊ถ ํ์ฝ์ ์ํด ๋ณดํธ๋ฉ๋๋ค.
- AI ์์ฑ ์ปจํ ์ธ ์ ๋ฒ์ ์ง์: ๋ณธ AI ์์ฑ ์ปจํ ์ธ ๋ ์ฌ๋ฅ๋ท์ ์ง์ ์ฐฝ์๋ฌผ๋ก ์ธ์ ๋๋ฉฐ, ๊ด๋ จ ๋ฒ๊ท์ ๋ฐ๋ผ ์ ์๊ถ ๋ณดํธ๋ฅผ ๋ฐ์ต๋๋ค.
- ์ฌ์ฉ ์ ํ: ์ฌ๋ฅ๋ท์ ๋ช ์์ ์๋ฉด ๋์ ์์ด ๋ณธ ์ปจํ ์ธ ๋ฅผ ๋ณต์ , ์์ , ๋ฐฐํฌ, ๋๋ ์์ ์ ์ผ๋ก ํ์ฉํ๋ ํ์๋ ์๊ฒฉํ ๊ธ์ง๋ฉ๋๋ค.
- ๋ฐ์ดํฐ ์์ง ๊ธ์ง: ๋ณธ ์ปจํ ์ธ ์ ๋ํ ๋ฌด๋จ ์คํฌ๋ํ, ํฌ๋กค๋ง, ๋ฐ ์๋ํ๋ ๋ฐ์ดํฐ ์์ง์ ๋ฒ์ ์ ์ฌ์ ๋์์ด ๋ฉ๋๋ค.
- AI ํ์ต ์ ํ: ์ฌ๋ฅ๋ท์ AI ์์ฑ ์ปจํ ์ธ ๋ฅผ ํ AI ๋ชจ๋ธ ํ์ต์ ๋ฌด๋จ ์ฌ์ฉํ๋ ํ์๋ ๊ธ์ง๋๋ฉฐ, ์ด๋ ์ง์ ์ฌ์ฐ๊ถ ์นจํด๋ก ๊ฐ์ฃผ๋ฉ๋๋ค.
์์ง ๋๊ธ์ด ์์ต๋๋ค.