You are here

public function ViewModesInventoryFactory::mapViewModeWithLayout in View Modes Inventory - Bootstrap Ready 8.2

Same name and namespace in other branches
  1. 8 src/ViewModesInventoryFactory.php \Drupal\vmi\ViewModesInventoryFactory::mapViewModeWithLayout()

Map a view mode with a layout and default configuration template.

Parameters

string $selected_view_mode: Selected view mode in the custom display settings form.

string $default_mapped_layout: Default mapped layout.

string $entity_type: Entity type like node, block, user.

string $bundle_name: Bundle name.

string $config_template_file: Config template file name.

string $config_name: Config name to map to.

File

src/ViewModesInventoryFactory.php, line 121

Class

ViewModesInventoryFactory
View Modes Inventory Factory.

Namespace

Drupal\vmi

Code

public function mapViewModeWithLayout($selected_view_mode, $default_mapped_layout, $entity_type, $bundle_name, $config_template_file, $config_name) {

  // Replace CONTENT_TYPE_NAME with the bundle name for the config name.
  $real_config_name = str_replace('CONTENT_TYPE_NAME', $bundle_name, $config_name);
  $view_mode_config = $this->configFactory
    ->getEditable($real_config_name);

  // Load the config template.
  $module_path = $this->moduleHandler
    ->getModule('vmi')
    ->getPath();
  $full_config_template_file = DRUPAL_ROOT . '/' . $module_path . $config_template_file;
  $config_template_content = file_get_contents($full_config_template_file);

  // Replace CONTENT_TYPE_NAME with the bundle name in the config template.
  $real_config_template_content = str_replace('CONTENT_TYPE_NAME', $bundle_name, $config_template_content);

  // Parse real config template conten to data.
  $real_config_template_content_data = (array) Yaml::parse($real_config_template_content);

  // Set and save new message value.
  $view_mode_config
    ->setData($real_config_template_content_data)
    ->save();
}