- CEO & Drupal dreamer at Ymbra
- Drupal community member
- @rvilar in twitter, drupal.org, IRC and mostly everywhere else
- ramon@ymbra.com






<?php if (isset($block_html_id)): ?>
<div id="<?php print $block_html_id; ?>" <?php print $attributes; ?>>
<?php else: ?>
<div <?php print $attributes; ?>>
<?php endif; ?>
<?php print render($title_prefix); ?>
<?php if ($label): ?>
<h2<?php print $title_attributes; ?>><?php print $label; ?></h2>
<?php endif;?>
<?php print render($title_suffix); ?>
<div<?php print $content_attributes; ?>>
<?php print render($content) ?>
</div>
</div>
<div{{ attributes }}>
{{ title_prefix }}
{% if label %}
<h2{{ title_attributes }}>{{ label }}</h2>
{% endif %}
{{ title_suffix }}
<div{{ content_attributes }}>
{{ content }}
</div>
</div>
abstract class ConfigurableActionBase extends ActionBase
implements ConfigurablePluginInterface, PluginFormInterface {
//
interface ConfigurablePluginInterface {
public function getConfiguration();
public function setConfiguration(array $configuration);
public function defaultConfiguration();
}
//
interface PluginFormInterface {
public function buildConfigurationForm(array $form, array &$form_state);
public function validateConfigurationForm(array &$form, array &$form_state);
public function submitConfigurationForm(array &$form, array &$form_state);
}
$action = entity_create('action', array(
'id' => 'user_add_role_action.' . $role->id(),
'type' => 'user',
'label' => t('Add the @label role to the selected users',
array('@label' => $role->label())),
'configuration' => array(
'rid' => $role->id(),
),
'plugin' => 'user_add_role_action',
));
$action->save();
migrate.migration.d6_user_role.yml
id: d6_user_role
source:
plugin: d6_user_role
process:
id:
-
plugin: machine_name
source: name
-
plugin: dedupe_entity
entity_type: user_role
field: id
-
plugin: user_update_8002
label: name
(...)
destination:
plugin: entity:user_role
/**
* This plugin creates a machine name.
*
* @MigrateProcessPlugin(
* id = "machine_name"
* )
*/
class MachineName extends ProcessPluginBase {
(...)
public function transform($value, MigrateExecutable $migrate_executable, Row $row, $destination_property) {
$new_value = $this->getTransliteration()->transliterate($value, Language::LANGCODE_DEFAULT, '_');
$new_value = strtolower($new_value);
$new_value = preg_replace('/[^a-z0-9_]+/', '_', $new_value);
return preg_replace('/_+/', '_', $new_value);
}
Simplified version of both email and telephone modules for Drupal 7
id: body
uuid: 7a39e1c1-9ed8-46a6-8c35-53603518dab9
status: '1'
langcode: en
type: text_with_summary
settings: { }
module: text
active: '1'
entity_types:
- node
storage:
type: field_sql_storage
settings: { }
module: field_sql_storage
active: '1'
locked: '0'
cardinality: '1'
translatable: '0'
id: node.article.body uuid: 12fa357f-45fb-4c58-b600-dbb984601811 status: '1' langcode: en field_uuid: 7a39e1c1-9ed8-46a6-8c35-53603518dab9 entity_type: node bundle: article label: Body description: '' required: '1' default_value: '' default_value_function: '' settings: text_processing: '1' display_summary: '1' field_type: text_with_summary
Drupal 7 has field_create_field(), field_create_instance(), update, delete and dozens of other field API crud functions and hooks.
$field = entity_create('field_entity', array(
'entity_type' => 'node',
'id' => 'field_image',
'type' => 'image',
'cardinality' => 1,
));
$field->save();
entity_create('field_instance', array(
'field_id' => 'field_image',
'entity_type' => 'node',
'bundle' => 'article',
))->save();
$field = entity_load('field_image');
$field->cardinality = 3;
$field->settings['foo'] = 'bar';
$field->save();
$field->delete();
+
hook_entity_save()
hook_entity_delete()
...
id: node.article.default
uuid: 4ac7c6b0-717e-43bd-8b04-4ad1e3f32429
targetEntityType: node
bundle: article
mode: default
content:
body:
label: hidden
type: text_default
settings: { }
field_image:
label: hidden
type: image
settings:
image_style: large
image_link: ''
weight: '-1'
$display = entity_get_display($entity_type, $bundle, $view_mode);
$display->setComponent('body', array(
'type' => 'text_default',
'weight' => 0,
))
-> save();
// During entity_view():
hook_entity_display_alter(EntityDisplay $display);
hook_entity_view(EntityInterface $entity,
EntityDisplay $display);
$options = $display->getComponent('field_image');
If you aren't happy
to see core adding form modes
then we can’t be friends
/**
* @Widget(
* id = "mymodule_widget",
* label = @Translation("My awesome widget"),
* field_types = {
* "text"
* },
* settings = {
* "some_setting" = "Some default value"
* }
* )
*/
class MyWidget extends WidgetBase {
class MyWidget extends WidgetBase {
public function settingsForm(array $form, array &$form_state);
public function formElement(array $items, array &$form, array &$form_state);
public function errorElement(array $element, array $error,
array $form, array &$form_state);
public function massageFormValues(array $values, array $form, array &$form_state);
}
class MyFormatter extends FormatterBase {
public function settingsForm(array $form, array &$form_state);
public function settingsSummary();
public function prepareView(array $entities, $langcode,array &$items);
public function view(EntityInterface $entity, $langcode, array $items);
}
class TextItem extends TextItemBase {
(abstract class TextItemBase extends ConfigFieldItemBase)
public static function schema(Field $field);
public function settingsForm(array $form, array &$form_state);
public function instanceSettingsForm(array $form, array &$form_state);
public function validate() / presave() / delete() ...
Field values are objects
$node->field_tags instanceof FieldInterface
$node->field_tags[0] instanceof FieldItemInterface
Improved Developer experience
$node->body[LANGUAGE_NONE][0]['value']
is now
$node->body->value
// or
$node->body[0]->value
$translation = $node->getTranslation('de');
// Access and update translated fields as usual:
$german_title = $translation->title->value;
$translation->title->value = 'Drupal spricht viele Sprachen!';
$node instanceof NodeInterface
$translation instanceof NodeInterface
$entity->getFieldDefinitions();
foreach ($entity as $field) {
foreach ($field as $field_item) {
$field_item->getPropertyDefinitions();
foreach ($field_item as $item_value) {
$is_string = $item_value instanceof StringInterface;
}
}
}
"Data type" plugins, include
$properties['title'] = array(
'label' => t('Title'),
'description' => t('The title of this node, always treated as non-markup plain text.'),
'type' => 'string_field',
);