Skip to content

Instantly share code, notes, and snippets.

@woprrr
Created November 3, 2017 14:36
Show Gist options
  • Save woprrr/f5fd15d65dc5385464259d4a4a85c5b7 to your computer and use it in GitHub Desktop.
Save woprrr/f5fd15d65dc5385464259d4a4a85c5b7 to your computer and use it in GitHub Desktop.
<?php
namespace Drupal\my_dummy_module;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Manipulates entity type information.
*
* This class contains primarily bridged hooks for compile-time or
* cache-clear-time hooks. Runtime hooks should be placed in EntityOperations.
*/
class EntityTypeInfo implements ContainerInjectionInterface {
/**
* Namespace of 'DUMMY FORM' NodeForm overrides.
*/
const MY_DUMMY_FORM = 'Drupal\my_dummy_module\Form\MyDummyForm';
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $currentUser;
/**
* EntityTypeInfo constructor.
*
* @param \Drupal\Core\Session\AccountInterface $current_user
* Current user.
*/
public function __construct(AccountInterface $current_user) {
$this->currentUser = $current_user;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('current_user')
);
}
/**
* Add new properties onto entity types.
*
* This is an alter hook bridge.
*
* @param \Drupal\Core\Entity\EntityTypeInterface[] $entity_types
* The master entity type list to alter.
*
* @see hook_entity_type_alter()
*/
public function entityTypeAlter(array &$entity_types) {
if ($entity_types['node']->getFormClass('dummy_mode')) {
$entity_types['node']->setFormClass('dummy_mode', static::MY_DUMMY_FORM);
}
}
}
name: My dummy module
type: module
description: Module presentation...
core: 8.x
package: CUSTOM
dependencies:
- drupal:form_mode_manager
<?php
/**
* @file
* The my_dummy_module module.
*/
use \Drupal\my_dummy_module\EntityTypeInfo;
/**
* Implements hook_entity_type_alter().
*/
function my_dummy_module_entity_type_alter(array &$entity_types) {
return \Drupal::service('class_resolver')
->getInstanceFromDefinition(EntityTypeInfo::class)
->entityTypeAlter($entity_types);
}
<?php
namespace Drupal\my_dummy_module\Form;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\node\NodeForm;
use Drupal\user\PrivateTempStoreFactory;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Form handler for the node content "dummy_mode" edit/add forms.
*/
class MyDummyForm extends NodeForm {
/**
* Constructs a MyDummyForm object.
*
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
* The entity manager.
* @param \Drupal\user\PrivateTempStoreFactory $temp_store_factory
* The factory for the temp store object.
* @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
* The entity type bundle service.
* @param \Drupal\Component\Datetime\TimeInterface $time
* The time service.
*/
public function __construct(EntityManagerInterface $entity_manager, PrivateTempStoreFactory $temp_store_factory, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, TimeInterface $time = NULL, AccountInterface $current_user) {
parent::__construct($entity_manager, $temp_store_factory, $entity_type_bundle_info, $time, $current_user);
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('entity.manager'),
$container->get('user.private_tempstore'),
$container->get('entity_type.bundle.info'),
$container->get('datetime.time'),
$container->get('current_user')
);
}
/**
* {@inheritdoc}
*
*/
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
$form['my_dummy_wrapper'] = [
'#type' => 'details',
'#title' => t('Wrapper custom'),
'#open' => TRUE,
'#attributes' => [
'class' => ['node-form-dummy'],
],
'#attached' => [
'library' => ['node/drupal.node'],
],
'#group' => 'advanced',
];
$form['field_image']['#group'] = 'my_dummy_wrapper';
return $form;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment