Plugins are small pieces of functionality that are swappable. Plugins that perform similar functionality are of the same plugin type.
Plugins are defined by modules: a module may provide plugins of different types, and different modules may provide their own plugins of a particular type.
namespace Drupal\hello_world\Plugin\Block;
use Drupal\Core\Block\BlockBase;
/**
* Provides a 'Hello' Block
*
* @Block(
* id = "hello_block",
* admin_label = @Translation("Hello block"),
* )
*/
class HelloBlock extends BlockBase {
/**
* {@inheritdoc}
*/
public function build() {
return [
'#markup' => $this->t('Hello, World!'),
];
}
}
Låter oss hantera entiteter på en högre nivå. Vi kan ladda enskilda entiteter, hämta formulär, url:er m.m.
Låter oss skriva detaljerade förfrågningar för att hämta en eller flera entiteter. Ungefär som att skriva en SQL-query fast för en Drupal-entitet.
/**
* Load a single entity.
*/
$color = \Drupal::entityTypeManager()
->getStorage('color')
->load('red');
/**
* Load all entities.
*/
$colors = \Drupal::EntityTypeManager()
->getStorage('colors')
->loadMultiple();
/**
* Load a entity form.
*/
$color_add_form = \Drupal::entityTypeManager()
->getFormObject('color', 'add');
/**
* First perform the query to get the entity ids.
*/
$ids = \Drupal::entityQuery('movie')
->sort('rating', 'DESC')
->range(0, 5)
->execute();
/**
* Then load the entities we found with the entity type manager.
*/
$movies = \Drupal::entityTypeManager()
->getStorage('movie')
->loadMultiple($ids);
/**
* First perform the query to load node ids.
*/
$ids = \Drupal::entityQuery('node')
->condition('type', 'article')
->condition('status', 1)
->sort('created', 'DESC')
->range(0, 10)
->execute();
/**
* Then load the node entities we found with the entity type manager.
*/
$nodes = \Drupal::entityTypeManager()
->getStorage('node')
->loadMultiple($ids);