2

I'm trying to rebuild the pullquote module in Drupal 8 but am running into an issue accessing view modes. In the Drupal 7 version, the view_modes variable is set by the following lines:

$entity_info = entity_get_info('node');
$view_modes = $entity_info['view modes'];
$view_mode_options = array();

foreach ($view_modes as $mode => $settings) {
  $view_mode_options[$mode] = $settings['label'];
}

However, entity_get_info() has been depreciated in Drupal 8, and according to the documentation I found, replaced with \Drupal::entityTypeManager()->getDefinition().

So I tried replacing the first line:

$nodeEntity = \Drupal::entityTypeManager()->getDefinition('node');

but the array was empty, so maybe I'm using the wrong tool.

I then found this answer for accessing a list of all view nodes.

$view_modes = \Drupal::entityQuery('entity_view_mode')
  ->condition('targetEntityType', 'node')
  ->execute();

It worked, however, they were returned like this:

['node.full', 'node.rss', 'node.teaser']

But here's the problem. I'm using these view modes as options in a form. When the form is saved in configuration, having a key with a period breaks the YAML file with the following error: The website encountered an unexpected error. Please try again later. Drupal\Core\Config\ConfigValueException: node.full key contains a dot which is not supported. in Drupal\Core\Config\ConfigBase->validateKeys(). I'm confused though, because I map over the array so that I'm stripping the names of ".node"

Here's my full code for reference:

// Load the list of avialable view modes.
$view_modes = \Drupal::entityQuery('entity_view_mode')
  ->condition('targetEntityType', 'node')
  ->execute();

// remove "node."
$view_mode_options = array_map(function ($mode) {
  $parsed = substr($mode, 5);
  return $parsed;
}, $view_modes);

$form['view_mode_settings']['pullquote_view_modes'] = [
  '#type' => 'checkboxes',
  '#title' => t('View Modes'),
  '#options' => $view_mode_options,
  '#default_value' => $config->get('pullquote_view_modes'),
  '#description' => t('Choose which view modes will load pullquote code and render pullquotes. For better performance you should only load pullquote where you need it.'),
   '#states' => array(
     'visible' => array(
       ':input[name="pullquote_load_by_view_mode"]' => array('value' => 1),
       ':input[name="pullquote_scope"]' => array('value' => 0),
     ),
   ),
];

Now the view modes display properly on the page, but I still get the error that Drupal can't save "node.full" in configuration, which is confusing because the page displays only "full."

Is there a method for getting the label of different entity types? Or is there a way to actually parse "node.full" down to a string that is only "full"--both when it displays and how Drupal understands it?

2

Drupal 8 services are essentially helpful objects (tools), which live inside the services container (tool belt). Calling a service returns an object, essentially providing a clear path to the helper methods within that class. As previously answered, EntityDisplayRepository class contains various methods, specifically one that will return an array of entity view modes (see a list of available methods). By calling \Drupal::service('entity_display.repository') and storing it in a variable, you now have an object that you can then call EntityDisplayRepository methods from, which in your case, is to return 'node' view modes.

/**
 * Implements hook_preprocess_page().
 *
 * This function is called before the page is rendered.
 */
function modulename_preprocess_page(&$vars) {
  // call the entity service.
  $nodeEntity = \Drupal::service('entity_display.repository');

  // call the necessary method in order to return 'node' view modes.
  $nodeEntity->getViewModes('node');

  // one line: \Drupal::service('entity_display.repository')->getViewModes('node');
}
2

You are looking for \Drupal\Core\Entity\EntityDisplayRepository::getViewModeOptions() aka the service \Drupal::service('entity_display.repository'), that gives you an array with the label.

  • Can you be a little more explicit in how to use this? I tried $view_modes = \Drupal\Core\Entity\EntityDisplayRepository::getViewModeOptions('node'); and got the error "Error: Using $this when not in object context in Drupal\Core\Entity\EntityDisplayRepository::getViewModeOptions() (line 154 of core/lib/Drupal/Core/Entity/EntityDisplayRepository.php)" – crash springfield Mar 29 '18 at 11:58
  • You need to call that on the service, not the class. – Berdir Mar 29 '18 at 13:08
  • Can you give an example? What is the service? What is the class? – crash springfield Mar 29 '18 at 13:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.