This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Drupal\Core\Render\Renderer; | |
use Drupal\Core\Ajax\AjaxResponse; | |
use Drupal\Core\Form\FormBuilder; | |
/** | |
* Drupal\Core\Block\BlockManager definition. | |
* | |
* @var \Drupal\Core\Block\BlockManager | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Drupal\Core\Ajax\AjaxResponse; | |
use Drupal\views\Views; | |
use Drupal\Core\Render\Renderer | |
/** | |
* Drupal\Core\Render\Renderer definition. | |
* | |
* @var \Drupal\Core\Render\Renderer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function __callURLinBackground($url) { | |
exec("wget -bqc ".$url." -P /var/www/html/", $output, $result_code); | |
echo $output; | |
echo $result_code; | |
echo '<pre>';print_r($output); | |
echo '<pre>';print_r($result_code); | |
echo 'Running in background'; | |
} | |
// call a URL in background |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Implements hook_views_query_alter(). | |
*/ | |
function mymodule_announcements_views_query_alter(Drupal\views\ViewExecutable $view, Drupal\views\Plugin\views\query\QueryPluginBase $query) { | |
// Alter annoucements views. | |
if (($view->id() == 'announcements')) { | |
if ($view->current_display == 'page_announcements' || $view->current_display == 'block_announcements') { | |
$current_user_id = \Drupal::currentUser()->id(); | |
$user_service = \Drupal::service('my_service.user'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Define the interface for a payment gateway | |
interface PaymentGateway { | |
public function processPayment(float $amount): bool; | |
} | |
// Define a class for a credit card payment gateway that implements the PaymentGateway interface | |
class CreditCardGateway implements PaymentGateway { | |
private string $apiKey; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// In this example, we define a trait called Greetings which contains two methods: sayHello() and sayGoodbye(). | |
// We then define a class called Person and use the Greetings trait within it using the use keyword. | |
// This allows the Person class to have access to the methods defined in the Greetings trait. | |
// We create an instance of the Person class and call the sayHello() and sayGoodbye() methods, | |
// which output "Hello!" and "Goodbye!", respectively. | |
trait Greetings { | |
public function sayHello() { | |
echo "Hello!"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Drupal\media\Entity\Media; | |
use Drupal\file\Entity\File; | |
use Drupal\Core\Url; | |
// Load the media | |
$media_id = <Replace with the id of media entity>; | |
$media = Media::load($media_id); | |
// Get file id from the media. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Generic function to direct download any type of file. | |
*/ | |
function directDownloadAnyFile($file, $newfilename = '', $mimetype='', $isremotefile = false) { | |
$formattedhpath = ""; | |
$filesize = ""; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// The splat operator (also known as the variadic operator) in PHP is represented by three dots (...) | |
// It allows you to pass an arbitrary number of arguments to a function or method. | |
// Here's an example of using the splat operator in PHP: | |
function addNumbers(...$numbers) { | |
$sum = 0; | |
foreach ($numbers as $number) { | |
$sum += $number; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Named arguments were introduced in PHP 8, | |
// and they allow you to pass arguments to a function or method by specifying the name of the argument | |
// rather than relying on the order in which the arguments are defined. | |
// Here's an example of using named arguments in PHP: | |
function createUser($name, $email, $age) { | |
echo "Name: $name <br>"; |