#php #forms #drupal #drupal-8 #drupal-modules
#php #формы #drupal #drupal-8 #drupal-модули
Вопрос:
Я создал небольшой пользовательский модуль в Drupal 8 для модерации новых объектов и обновления объектов.
Модуль прост. Он помечает с помощью модуля flag объекты, созданные или обновленные автоматически.
Вот мой модуль :
<?php
/**
* @file
* Holds hook implementation for the Admin Task Notify module.
*/
use DrupalflagFlaggingInterface;
use DrupalCoreEntityContentEntityBase;
use DrupaluserEntityUser;
use Drupalcommerce_storeEntityStore;
use Drupalcommerce_productEntityProduct;
use DrupalgroupEntityGroup;
use DrupalnodeEntityNode;
use DrupalcommentEntityComment;
/**
* Implements hook_user_insert() and hook_user_update().
*/
function admin_task_notify_form_alter(amp;$form, amp;$form_state, $form_id) {
if ($form_id == 'user_register_form' || $form_id == 'user_form') {
$form['actions']['submit']['#submit'][] = 'admin_task_notify_flag_user';
}
}
function admin_task_notify_flag_user(amp;$form, DrupalCoreFormFormStateInterface $form_state) {
$flag_id = 'moderate_user';
$flag_service = Drupal::service('flag');
$flag = $flag_service->getFlagById($flag_id);
$user_admin = DrupaluserEntityUser::load(1);
$user_to_flag_uid = $form_state->getValue('uid');
$user_to_flag = DrupaluserEntityUser::load($user_to_flag_uid);
$is_admin = $user_to_flag->hasRole('administrator');
$flag_status = $flag_service->getFlagging($flag, $user_to_flag, $user_admin);
if (!$flag_status amp;amp; !$is_admin) {
$flag_service->flag($flag, $user_to_flag, $user_admin);
}
}
/**
* Implements hook_store_insert() and hook_store_update().
*/
function admin_task_notify_commerce_store_insert(Store $commerce_store) {
if ($commerce_store->bundle() == 'ephemere' || $commerce_store->bundle() == 'personnel' || $commerce_store->bundle() == 'professionnel') {
$flag_id = 'moderate_store';
$flag_service = Drupal::service('flag');
$flag = $flag_service->getFlagById($flag_id);
$user_admin = DrupaluserEntityUser::load(1);
$flag_status = $flag_service->getFlagging($flag, $commerce_store, $user_admin);
if (!$flag_status) {
$flag_service->flag($flag, $commerce_store, $user_admin);
}
}
}
function admin_task_notify_commerce_store_update(Store $commerce_store) {
if ($commerce_store->bundle() == 'ephemere' || $commerce_store->bundle() == 'personnel' || $commerce_store->bundle() == 'professionnel') {
$flag_id = 'moderate_store';
$flag_service = Drupal::service('flag');
$flag = $flag_service->getFlagById($flag_id);
$user_admin = DrupaluserEntityUser::load(1);
$flag_status = $flag_service->getFlagging($flag, $commerce_store, $user_admin);
if (!$flag_status) {
$flag_service->flag($flag, $commerce_store, $user_admin);
}
}
}
/**
* Implements hook_product_insert() and hook_product_update().
*/
function admin_task_notify_commerce_product_insert(Product $commerce_product) {
if ($commerce_product->bundle() == 'produit' || $commerce_product->bundle() == 'service') {
$flag_id = 'moderate_product';
$flag_service = Drupal::service('flag');
$flag = $flag_service->getFlagById($flag_id);
$user_admin = DrupaluserEntityUser::load(1);
$flag_status = $flag_service->getFlagging($flag, $commerce_product, $user_admin);
if (!$flag_status) {
$flag_service->flag($flag, $commerce_product, $user_admin);
}
}
}
function admin_task_notify_commerce_product_update(Product $commerce_product) {
if ($commerce_product->bundle() == 'produit' || $commerce_product->bundle() == 'service') {
$flag_id = 'moderate_product';
$flag_service = Drupal::service('flag');
$flag = $flag_service->getFlagById($flag_id);
$user_admin = DrupaluserEntityUser::load(1);
$flag_status = $flag_service->getFlagging($flag, $commerce_product, $user_admin);
if (!$flag_status) {
$flag_service->flag($flag, $commerce_product, $user_admin);
}
}
}
/**
* Implements hook_group_insert() and hook_group_update().
*/
function admin_task_notify_group_insert(Group $group) {
if ($group->bundle() == 'personnel' || $group->bundle() == 'professionnel') {
$flag_id = 'moderate_group';
$flag_service = Drupal::service('flag');
$flag = $flag_service->getFlagById($flag_id);
$user_admin = DrupaluserEntityUser::load(1);
$flag_status = $flag_service->getFlagging($flag, $group, $user_admin);
if (!$flag_status) {
$flag_service->flag($flag, $group, $user_admin);
}
}
}
function admin_task_notify_group_update(Group $group) {
if ($group->bundle() == 'personnel' || $group->bundle() == 'professionnel') {
$flag_id = 'moderate_group';
$flag_service = Drupal::service('flag');
$flag = $flag_service->getFlagById($flag_id);
$user_admin = DrupaluserEntityUser::load(1);
$flag_status = $flag_service->getFlagging($flag, $group, $user_admin);
if (!$flag_status) {
$flag_service->flag($flag, $group, $user_admin);
}
}
}
/**
* Implements hook_node_insert() and hook_node_update().
*/
function admin_task_notify_node_insert(Node $node) {
if ($node->bundle() == 'annonce' || $node->bundle() == 'article' || $node->bundle() == 'demande' || $node->bundle() == 'discussion' || $node->bundle() == 'evenement' || $node->bundle() == 'itineraire' || $node->bundle() == 'recipe') {
$flag_id = 'moderate_node';
$flag_service = Drupal::service('flag');
$flag = $flag_service->getFlagById($flag_id);
$user_admin = DrupaluserEntityUser::load(1);
$flag_status = $flag_service->getFlagging($flag, $node, $user_admin);
if (!$flag_status) {
$flag_service->flag($flag, $node, $user_admin);
}
}
}
function admin_task_notify_node_update(Node $node) {
if ($node->bundle() == 'annonce' || $node->bundle() == 'article' || $node->bundle() == 'demande' || $node->bundle() == 'discussion' || $node->bundle() == 'evenement' || $node->bundle() == 'itineraire' || $node->bundle() == 'recipe') {
$flag_id = 'moderate_node';
$flag_service = Drupal::service('flag');
$flag = $flag_service->getFlagById($flag_id);
$user_admin = DrupaluserEntityUser::load(1);
$flag_status = $flag_service->getFlagging($flag, $node, $user_admin);
if (!$flag_status) {
$flag_service->flag($flag, $node, $user_admin);
}
}
}
/**
* Implements hook_comment_insert() and hook_comment_update().
*/
function admin_task_notify_comment_insert(Comment $comment) {
if ($comment->bundle() == 'annonce' || $comment->bundle() == 'article' || $comment->bundle() == 'demande' || $comment->bundle() == 'discussion' || $comment->bundle() == 'evenement' || $comment->bundle() == 'itineraire' || $comment->bundle() == 'recipe' || $comment->bundle() == 'produit' || $comment->bundle() == 'service') {
$flag_id = 'moderate_comment';
$flag_service = Drupal::service('flag');
$flag = $flag_service->getFlagById($flag_id);
$user_admin = DrupaluserEntityUser::load(1);
$flag_status = $flag_service->getFlagging($flag, $comment, $user_admin);
if (!$flag_status) {
$flag_service->flag($flag, $comment, $user_admin);
}
}
}
function admin_task_notify_comment_update(Comment $comment) {
if ($comment->bundle() == 'annonce' || $comment->bundle() == 'article' || $comment->bundle() == 'demande' || $comment->bundle() == 'discussion' || $comment->bundle() == 'evenement' || $comment->bundle() == 'itineraire' || $comment->bundle() == 'recipe' || $comment->bundle() == 'produit' || $comment->bundle() == 'service') {
$flag_id = 'moderate_comment';
$flag_service = Drupal::service('flag');
$flag = $flag_service->getFlagById($flag_id);
$user_admin = DrupaluserEntityUser::load(1);
$flag_status = $flag_service->getFlagging($flag, $comment, $user_admin);
if (!$flag_status) {
$flag_service->flag($flag, $comment, $user_admin);
}
}
}
Это то, что я хочу сделать :
Я хочу автоматически помечать хранилище payment_gateways
флагом при commerce_store_payment_gateways_form
отправке формы.
Я хочу автоматически помечать хранилище shipping_methods
флагом при commerce_store_shipping_methods_form
отправке формы.
Как я могу это сделать? Какой код я должен добавить в свой модуль?
Я запустил некоторый код, но я не знаю, правильный ли он и где его интегрировать в мой модуль.
Я не знаю, есть ли другой способ сделать это.
/**
* Payment Gateways.
*/
if ($commerce_store->bundle() == 'professionnel') {
if ($form_id == 'commerce_store_payment_gateways_form') {
$flag_id = 'payment_gateways';
$flag_service = Drupal::service('flag');
$flag = $flag_service->getFlagById($flag_id);
$user_admin = DrupaluserEntityUser::load(1);
$flag_status = $flag_service->getFlagging($flag, $commerce_store, $user_admin);
if (!$flag_status) {
$flag_service->flag($flag, $commerce_store, $user_admin);
}
}
}
/**
* Shipping Methods.
*/
if ($commerce_store->bundle() == 'professionnel') {
if ($form_id == 'commerce_store_shipping_methods_form') {
$flag_id = 'shipping_methods';
$flag_service = Drupal::service('flag');
$flag = $flag_service->getFlagById($flag_id);
$user_admin = DrupaluserEntityUser::load(1);
$flag_status = $flag_service->getFlagging($flag, $commerce_store, $user_admin);
if (!$flag_status) {
$flag_service->flag($flag, $commerce_store, $user_admin);
}
}
}
Ответ №1:
Вы можете использовать hook_form_FORM_ID_alter
для добавления пользовательского обработчика отправки в свои формы:
/*
* Implements hook_form_FORM_ID_alter
*/
function hook_form_commerce_store_payment_gateways_form_alter(amp;$form, DrupalCoreFormFormStateInterface $form_state, $form_id) {
foreach (array_keys($form['actions']) as $action) {
if ($action != 'preview' amp;amp; isset($form['actions'][$action]['#type']) amp;amp; $form['actions'][$action]['#type'] === 'submit') {
$form['actions'][$action]['#submit'][] = 'custom_form_submit_handler';
}
}
}
Затем поместите свою логику пометки в пользовательский обработчик:
function custom_form_submit_handler($form, DrupalCoreFormFormStateInterface $form_state) {
$flag_id = 'payment_gateways';
$flag_service = Drupal::service('flag');
$flag = $flag_service->getFlagById($flag_id);
$user_admin = DrupaluserEntityUser::load(1);
$flag_status = $flag_service->getFlagging($flag, $commerce_store, $user_admin);
if (!$flag_status) {
$flag_service->flag($flag, $commerce_store, $user_admin);
}
}
Сделайте то же самое для формы commerce_store_shipping_methods_form
.