Commit 174fa6ba authored by APolitsin's avatar APolitsin
Browse files

hooks refactoring

parent 637f0f48
<?php
namespace Drupal\synhelper\Hook;
/**
* @file
* Contains \Drupal\synhelper\Hook\EntityPresave.
*/
use Drupal\Core\Controller\ControllerBase;
/**
* Controller routines for page example routes.
*/
class EntityPresave extends ControllerBase {
/**
* Page Callback.
*/
public static function hook($entity) {
if ($bundle = self::checkBundle($entity)) {
if (!$entity->name->value) {
$title = "$bundle - " . format_date(REQUEST_TIME, 'long');
$entity->name->setValue($title);
}
}
if ($type = self::checkNode($entity)) {
if (!$entity->title->value) {
$title = "$type - " . format_date(REQUEST_TIME, 'long');
$entity->title->setValue($title);
}
}
}
/**
* Check Bundle.
*/
public static function checkBundle($entity) {
$result = FALSE;
if (method_exists($entity, 'bundle')) {
if (property_exists($entity, 'name')) {
$reflector = new \ReflectionClass(get_class($entity));
if ($reflector->getProperty('name')->isPublic()) {
$result = $entity->bundle();
}
}
}
return $result;
}
/**
* Check node.
*/
public static function checkNode($entity) {
$result = FALSE;
if (method_exists($entity, 'getType')) {
if (property_exists($entity, 'title')) {
$reflector = new \ReflectionClass(get_class($entity));
if ($reflector->getProperty('title')->isPublic()) {
$result = $entity->getType();
}
}
}
return $result;
}
}
<?php
namespace Drupal\synhelper\Hook;
use Drupal\Core\Controller\ControllerBase;
/**
* AjaxContactForm.
*/
class FormAlter extends ControllerBase {
/**
* Hook.
*/
public static function hook(&$form, &$form_state, $form_id) {
// Создание и редактирование материала.
if (strpos($form_id, 'node_') === 0) {
// Meta-data
if (isset($form['meta'])) {
$form['meta']['#access'] = FALSE;
}
// Revision Log - спрятать.
if (isset($form['revision_log'])) {
$form['revision_log']['#type'] = 'hidden';
$form['revision_information']['#group'] = FALSE;
$form['revision_information']['#open'] = FALSE;
}
// Path - спрятать.
if (isset($form['path_settings'])) {
$form['path_settings']['#group'] = FALSE;
$form['path_settings']['#open'] = FALSE;
}
// Attach - свернуть.
if (isset($form['field_attach'])) {
$form['field_attach']['widget']['#open'] = FALSE;
}
// Gallery - свернуть.
if (isset($form['field_gallery'])) {
$form['field_gallery']['widget']['#open'] = FALSE;
}
}
}
}
<?php
namespace Drupal\synhelper\Hook;
use Drupal\Core\Controller\ControllerBase;
/**
* FormNodeFormAlter.
*/
class FormNodeFormAlter extends ControllerBase {
/**
* Hook.
*/
public static function hook(&$form, &$form_state, $form_id) {
$formkey = str_replace("_edit", "", $form_id);
$type = substr($formkey, 5, -5);
if ($type == 'page') {
// Node-page form.
$form['menu']['#weight'] = -10;
$form['menu']['#group'] = FALSE;
$form['menu']['#open'] = FALSE;
$form['menu']['enabled']['#default_value'] = FALSE;
$form['menu']['link']['description']['#type'] = 'hidden';
$form['menu']['link']['weight']['#type'] = 'hidden';
$form['menu']['link']['menu_parent']['#prefix'] = '<div class="element-hidden">';
$form['menu']['link']['menu_parent']['#disabled'] = FALSE;
$form['menu']['link']['menu_parent']['#suffix'] = '</div>';
}
// Meta-data.
if (isset($form['meta'])) {
$form['meta']['#access'] = FALSE;
}
// Revision Log - hide.
if (isset($form['revision_log'])) {
$form['revision_log']['#type'] = 'hidden';
$form['revision_information']['#group'] = FALSE;
$form['revision_information']['#open'] = FALSE;
}
// Path - collapse.
if (isset($form['path_settings'])) {
$form['path_settings']['#group'] = FALSE;
$form['path_settings']['#open'] = FALSE;
}
// Attach - collapse.
if (isset($form['field_attach'])) {
$form['field_attach']['widget']['#open'] = FALSE;
}
// Gallery - collapse.
if (isset($form['field_gallery'])) {
$form['field_gallery']['widget']['#open'] = FALSE;
}
}
}
<?php
namespace Drupal\synhelper\Hook;
use Drupal\Core\Controller\ControllerBase;
/**
* AjaxContactForm.
*/
class FormNodePageEditFormAlter extends ControllerBase {
/**
* Hook.
*/
public static function hook(&$form, &$form_state, $form_id) {
// Редактирование страницы.
$form['menu']['#weight'] = -10;
$form['menu']['#group'] = FALSE;
$form['menu']['#open'] = FALSE;
// Old? $form['menu']['enabled']['#default_value'] = FALSE; !
$form['menu']['link']['description']['#type'] = 'hidden';
$form['menu']['link']['weight']['#type'] = 'hidden';
$form['menu']['link']['menu_parent']['#prefix'] = '<div class="element-hidden">';
$form['menu']['link']['menu_parent']['#disabled'] = FALSE;
$form['menu']['link']['menu_parent']['#suffix'] = '</div>';
}
}
<?php
namespace Drupal\synhelper\Hook;
use Drupal\Core\Controller\ControllerBase;
/**
* AjaxContactForm.
*/
class FormNodePageFormAlter extends ControllerBase {
/**
* Hook.
*/
public static function hook(&$form, &$form_state, $form_id) {
// Создание страницы.
$form['menu']['#weight'] = -10;
$form['menu']['#group'] = FALSE;
$form['menu']['#open'] = FALSE;
$form['menu']['enabled']['#default_value'] = FALSE;
$form['menu']['link']['description']['#type'] = 'hidden';
$form['menu']['link']['weight']['#type'] = 'hidden';
$form['menu']['link']['menu_parent']['#prefix'] = '<div class="element-hidden">';
$form['menu']['link']['menu_parent']['#disabled'] = FALSE;
$form['menu']['link']['menu_parent']['#suffix'] = '</div>';
}
}
<?php
namespace Drupal\synhelper\Hook;
/**
* @file
* Contains \Drupal\synhelper\Hook\NodePresave.
*/
use Drupal\Core\Controller\ControllerBase;
/**
* Controller NodePresave.
*/
class NodePresave extends ControllerBase {
/**
* Hook.
*/
public static function hook($node) {
// Set Node-title id empty.
if (!$node->title->value) {
$title = $node->getType() . " - " . format_date(REQUEST_TIME, 'long');
$node->title->setValue($title);
}
}
}
......@@ -8,19 +8,17 @@
use Drupal\synhelper\Hook\ContactMessagePresave;
use Drupal\synhelper\Hook\PreprocessHtml;
use Drupal\synhelper\Hook\FileValidate;
use Drupal\synhelper\Hook\FormAlter;
use Drupal\synhelper\Hook\FormContactMailSettingsAlter;
use Drupal\synhelper\Hook\FormContactMessageFormAlter;
use Drupal\synhelper\Hook\FormMenuEditFormAlter;
use Drupal\synhelper\Hook\FormNodePageFormAlter;
use Drupal\synhelper\Hook\FormNodePageEditFormAlter;
use Drupal\synhelper\Hook\EntityPresave;
use Drupal\synhelper\Hook\NodePresave;
use Drupal\synhelper\Hook\CssAlter;
use Drupal\synhelper\Hook\PhpmailAlterFromAlter;
use Drupal\synhelper\Hook\FormUpdateManagerInstallFormAlter;
use Drupal\synhelper\Hook\FormFieldUiFieldStorageAddFormAlter;
use Drupal\synhelper\Hook\FormNodeTypeAddFormAlter;
use Drupal\synhelper\Hook\FormFieldConfigEditFormAlter;
use Drupal\synhelper\Hook\FormNodeFormAlter;
/**
* Implements hook_cmlexchange_orders_query_alter().
......@@ -30,10 +28,10 @@ function synhelper_phpmail_alter_from_alter(&$mail) {
}
/**
* Implements hook_entity_presave().
* Implements hook_ENTITY_TYPE_presave().
*/
function synhelper_entity_presave($entity) {
EntityPresave::hook($entity);
function synhelper_node_presave($entity) {
NodePresave::hook($entity);
}
/**
......@@ -99,20 +97,6 @@ function synhelper_form_menu_edit_form_alter(&$form, $form_state, $form_id) {
FormMenuEditFormAlter::hook($form, $form_state, $form_id);
}
/**
* Implements hook_form_FORM_ID_alter() for node_page_form().
*/
function synhelper_form_node_page_form_alter(&$form, $form_state, $form_id) {
FormNodePageFormAlter::hook($form, $form_state, $form_id);
}
/**
* Implements hook_form_FORM_ID_alter() for node_page_edit_form().
*/
function synhelper_form_node_page_edit_form_alter(&$form, $form_state, $form_id) {
FormNodePageEditFormAlter::hook($form, $form_state, $form_id);
}
/**
* Contact Message form alter.
*/
......@@ -128,8 +112,8 @@ function synhelper_form_contact_mail_settings_alter(&$form, $form_state, $form_i
}
/**
* Implements hook_form_alter().
* Implements hook_form_TYPE_alter().
*/
function synhelper_form_alter(&$form, $form_state, $form_id) {
FormAlter::hook($form, $form_state, $form_id);
function synhelper_form_node_form_alter(&$form, $form_state, $form_id) {
FormNodeFormAlter::hook($form, $form_state, $form_id);
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment