ViewModesInventoryFactory.php in View Modes Inventory - Bootstrap Ready 8
File
src/ViewModesInventoryFactory.php
View source
<?php
namespace Drupal\vmi;
use Symfony\Component\Yaml\Yaml;
class ViewModesInventoryFactory {
public function __construct() {
parent::__construct();
}
public static function getViewModesList() {
$vmi_filename = \Drupal::root() . '/' . drupal_get_path('module', 'vmi') . '/src/assets/view_modes.list.vmi.yml';
if (is_file($vmi_filename)) {
$vmi_list = (array) Yaml::parse(file_get_contents($vmi_filename));
return $vmi_list;
}
else {
throw new \Exception('View modes inventory layouts list file does not exist!');
}
}
public static function getLayoutsMapping() {
$vmi_layout_filename = \Drupal::root() . '/' . drupal_get_path('module', 'vmi') . '/src/assets/layouts.mapping.vmi.yml';
if (is_file($vmi_layout_filename)) {
$vmi_layout_list = (array) Yaml::parse(file_get_contents($vmi_layout_filename));
return $vmi_layout_list;
}
else {
throw new \Exception('View modes inventory layouts list file does not exist!');
}
}
public static function mapViewModeWithLayout($selected_view_mode, $default_mapped_layout, $entity_type, $bundle_name, $config_template_file, $config_name) {
$real_config_name = str_replace('CONTENT_TYPE_NAME', $bundle_name, $config_name);
$view_mode_config = \Drupal::service('config.factory')
->getEditable($real_config_name);
$full_config_template_file = \Drupal::root() . '/' . drupal_get_path('module', 'vmi') . $config_template_file;
$config_template_content = file_get_contents($full_config_template_file);
$real_config_template_content = str_replace('CONTENT_TYPE_NAME', $bundle_name, $config_template_content);
$real_config_template_content_data = (array) Yaml::parse($real_config_template_content);
$view_mode_config
->setData($real_config_template_content_data)
->save();
}
}