http://jsbalsera.github.io/modes_and_formatters/
(And the code is in the other branches)
By Jesús Sanchez Balsera
@jsbalsera (Drupal.org, Twitter, etc.)
if (isset($var['node'])) {
$node = $var['node'];
}
elseif (arg(0) == 'node' && is_numeric(arg(1)) && arg(2) !== 'edit') {
$node = node_load(arg(1));
}
if (isset($node) && isset($node->type) && $node->type == 'article') {
}
if (!empty($node) && "article" == $node->type) {
if (arg(0) == 'node') {
unset($node->content['field_integers']);
}
}
I won't show examples of that.
function mymodule_entity_info_alter(&$entity_info) {
$entity_info['node']['view modes'] += array(
'example_mode' => array(
'label' => t('Example view mode'),
'custom settings' => TRUE,
)
);
}
Don't forget to clear the cache...
function hook_theme_preprocess_node(&$vars) {
$vars['theme_hook_suggestions'][] = 'node__' . $vars['view_mode'];
$vars['theme_hook_suggestions'][] = 'node__' . $vars['type'] .
'__' . $vars['view_mode'];
}
function mymodule_field_formatter_info() {
return array(
'mymodule_only_one' => array(
'label' => t('Only one item'),
'description' => t('Displays only the first item of a multievaluated one'),
'field types' => array('number_integer', 'number_decimal', 'number_float'),
'settings' => array(),
),
);
}
function mymodule_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
if ($items) {
switch ($display['type']) {
case 'mymodule_only_one':
$item = array_shift($items);
$element[0] = array('#markup' => $item['value']);
break;
}
}
return $element;
}
function mymodule_field_formatter_info() {
return array(
'mymodule_addition' => array(
'label' => t('Addition Formatter'),
'description' => t('Displays the result of the addition of all the field items'),
'field types' => array('number_integer', 'number_decimal', 'number_float'),
'settings' => array(),
),
);
}
function mymodule_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
if ($items) {
switch ($display['type']) {
case 'mymodule_addition':
$result = 0;
foreach ($items as $item) {
$result += $item['value'];
}
$element[0] = array('#markup' => $result);
break;
}
}
return $element;
}
// hook_field_formatter_info
'mymodule_headline' => array(
'label' => t('Headline'),
'descriptions' => t('Display a headline as a Hx (H1, H2, H3...) HTML tag'),
'field types' => array('text'),
'settings' => array(
'headline_type' => 'H3',
),
),
function mymodule_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$element = array();
if ($display['type'] == 'mymodule_headline') {
$element['headline_type'] = array(
'#title' => t('Headline Type'),
'#type' => 'select',
'#default_value' => $settings['headline_type'],
'#options' => array(
'H1' => 'H1', 'H2' => 'H2', 'H3' => 'H3',
),
'#required' => TRUE,
);
}
return $element;
}
function mymodule_field_formatter_settings_summary($field, $instance, $view_mode){
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$summary = '';
if ($display['type'] == 'mymodule_headline') {
$summary = t('Type: @headline_type', array(
'@headline_type' => $settings['headline_type']));
}
return $summary;
}
// hook_field_formatter_view().
case 'mymodule_headline':
$tag = $display['settings']['headline_type'];
if (!in_array($tag, array('H1', 'H2', 'H3'))) {
$tag = 'H3';
}
foreach ($items as $delta => $item) {
$element[$delta] = array(
'#markup' => format_string('!text!tag>', array(
'!tag' => strtolower($tag),
'!text' => $item['safe_value'],
)),
);
}
break;
//article.features.field_instance.inc
// Exported field_instance: 'node-article-field_text'
$field_instances['node-article-field_text'] = array(
'bundle' => 'article',
'default_value' => NULL,
'deleted' => 0,
'description' => '',
'display' => array(
'example_mode' => array(
'label' => 'hidden',
'module' => 'mymodule',
'settings' => array(
'headline_type' => 'H1',
),
'type' => 'mymodule_headline',
'weight' => 1,
),
// entity.view_mode.node.example_mode.yml
id: node.example_mode
label: 'Example mode'
targetEntityType: node
status: true
cache: true
uuid: f2776549-fc5d-49de-9600-bb8fee1d13d3
langcode: en
dependencies:
module:
- node
site/default/files/config_{...}/active/
id: node.article.example_mode
uuid: 0ed265dc-c5c8-4bbf-a500-bc87ee15c2f0
targetEntityType: node
bundle: article
mode: example_mode
content:
field_image:
type: image
weight: -1
settings:
image_style: large
image_link: ''
label: hidden
body:
type: text_default
weight: 0
settings: { }
label: hidden
field_tags:
type: taxonomy_term_reference_link
weight: 10
settings: { }
label: above
comment:
type: comment_default
weight: 20
settings:
pager_id: '0'
label: hidden
hidden:
langcode: true
status: true
.
├── lib
│ └── Drupal
│ └── mymodule
│ └── Plugin
│ └── Field
│ └── FieldFormatter
│ └── MymoduleHeadlineFormatter.php
└── mymodule.info.yml
/**
* Plugin implementation of the 'mymodule_headline' formatter.
*
* @FieldFormatter(
* id = "mymodule_headline",
* label = @Translation("Headline"),
* field_types = {
* "text",
* "text_long",
* },
* edit = {
* "editor" = "plain_text"
* }
* )
*/
class MymoduleHeadlineFormatter extends FormatterBase {