4 Best Drupal Hosting Providers in 2024

use Drupal\Core\Database\Database; // database connection instance. 
$connection = Database::getConnection();
// Get the 'mailer' service. 
$mailer = \Drupal::service('plugin.manager.mail');
use Symfony\Component\DependencyInjection\ContainerInterface;  
use Drupal\Core\Controller\ControllerBase;  

class MyController extends ControllerBase {  
  protected $myService;  

  public function __construct(MyServiceInterface $my_service) {  
    $this->myService = $my_service;  
  }  

  public static function create(ContainerInterface $container) {  
    return new static(  
      $container->get('my_module.my_service')  
    );  
  }  
}  
/**  
 * Implements hook_entity_insert().  
 */  
function mymodule_entity_insert(Drupal\Core\Entity\EntityInterface $entity) {  
  if ($entity->getEntityTypeId() == 'node') {  
    \Drupal::messenger()->addMessage('A new node has been created.');  
  }  
}  

Leave a Reply

Your email address will not be published. Required fields are marked *

×