Skip to content

Instantly share code, notes, and snippets.

@edutrul
Last active August 10, 2018 14:10
Show Gist options
  • Save edutrul/30efa2a059f7a1f032666c31d2b555fe to your computer and use it in GitHub Desktop.
Save edutrul/30efa2a059f7a1f032666c31d2b555fe to your computer and use it in GitHub Desktop.
Get drupal field formatter settings
<?php
$entity_type = 'node';
$bundle = 'layout';
$field_name = 'field_layout_region1_items';
$setting_name = 'promo';
// Get selected view modes for bundle
$view_modes = \Drupal::service('entity_display.repository')
->getViewModeOptionsByBundle(
$entity_type, $bundle
);
// Get format settings for field
foreach (array_keys($view_modes) as $view_mode) {
$settings = \Drupal::service('entity_type.manager')
->getStorage('entity_view_display')
->load($entity_type . '.' . $bundle . '.' . $view_mode)
->getRenderer($field_name);
// Get settings I need.
dpm($settings);
}
/*
OUTPUT:
Array
(
[link] => 1
)
Array
(
[link] => 1
)
stdClass Object
(
[__CLASS__] => Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceLabelFormatter
[fieldDefinition:protected] => stdClass Object
(
[__CLASS__] => Drupal\field\Entity\FieldConfig
[deleted:protected] =>
[fieldStorage:protected] =>
[id:protected] => node.layout.field_layout_region1_items
[field_name:protected] => field_layout_region1_items
[field_type:protected] => entity_reference
[entity_type:protected] => node
[bundle:protected] => layout
[label:protected] => Region 1 Items
[description:protected] =>
[settings:protected] => Array
(
[handler] => default:node
[handler_settings] => Array
(
[behaviors] => Array
(
[views-select-list] => Array
(
[status] => 0
)
)
[sort] => Array
(
[field] => _none
[direction] => ASC
)
[target_bundles] => Array
(
[promo] => promo
)
)
)
[required:protected] =>
[translatable:protected] =>
[default_value:protected] => Array
(
)
[default_value_callback:protected] =>
[itemDefinition:protected] =>
[constraints:protected] => Array
(
)
[propertyConstraints:protected] => Array
(
)
[originalId:protected] => node.layout.field_layout_region1_items
[status:protected] => 1
[uuid:protected] => 7d2cc7ea-820d-4811-b8c0-45e53ea7fa80
[isSyncing:Drupal\Core\Config\Entity\ConfigEntityBase:private] =>
[isUninstalling:Drupal\Core\Config\Entity\ConfigEntityBase:private] =>
[langcode:protected] => en
[third_party_settings:protected] => Array
(
)
[_core:protected] => Array
(
)
[trustedData:protected] =>
[entityTypeId:protected] => field_config
[enforceIsNew:protected] =>
[typedData:protected] =>
[cacheContexts:protected] => Array
(
)
[cacheTags:protected] => Array
(
)
[cacheMaxAge:protected] => -1
[_serviceIds:protected] => Array
(
)
[dependencies:protected] => Array
(
[config] => Array
(
[0] => field.storage.node.field_layout_region1_items
[1] => node.type.layout
[2] => node.type.promo
)
)
)
[settings:protected] => Array
(
[link] => 1
)
[label:protected] => above
[viewMode:protected] => default
[thirdPartySettings:protected] => Array
(
)
[defaultSettingsMerged:protected] =>
[pluginId:protected] => entity_reference_label
[pluginDefinition:protected] => Array
(
[field_types] => Array
(
[0] => entity_reference
)
[id] => entity_reference_label
[label] => stdClass Object
(
[__CLASS__] => Drupal\Core\StringTranslation\TranslatableMarkup
[translatedMarkup:protected] =>
[options:protected] => Array
(
)
[stringTranslation:protected] =>
[string:protected] => Label
[arguments:protected] => Array
(
)
)
[description] => stdClass Object
(
[__CLASS__] => Drupal\Core\StringTranslation\TranslatableMarkup
[translatedMarkup:protected] =>
[options:protected] => Array
(
)
[stringTranslation:protected] =>
[string:protected] => Display the label of the referenced entities.
[arguments:protected] => Array
(
)
)
[class] => Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceLabelFormatter
[provider] => core
[quickedit] => Array
(
[editor] => form
)
)
[configuration:protected] => Array
(
)
[stringTranslation:protected] =>
[_serviceIds:protected] => Array
(
)
)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment