You are here

public static function Utility::createFields in Basic cart 8

Same name and namespace in other branches
  1. 8.6 src/Utility.php \Drupal\basic_cart\Utility::createFields()
  2. 8.0 src/Utility.php \Drupal\basic_cart\Utility::createFields()
  3. 8.2 src/Utility.php \Drupal\basic_cart\Utility::createFields()
  4. 8.3 src/Utility.php \Drupal\basic_cart\Utility::createFields()
  5. 8.4 src/Utility.php \Drupal\basic_cart\Utility::createFields()
  6. 8.5 src/Utility.php \Drupal\basic_cart\Utility::createFields()
2 calls to Utility::createFields()
CartSettingsForm::submitForm in src/Form/CartSettingsForm.php
Form submission handler.
Utility::orderConnectFields in src/Utility.php

File

src/Utility.php, line 173
Contains \Drupal\basic_cart\Utility

Class

Utility

Namespace

Drupal\basic_cart

Code

public static function createFields($type = null) {
  $fields = $type == self::FIELD_ORDERCONNECT ? self::getFieldsConfig(self::FIELD_ORDERCONNECT) : self::getFieldsConfig();
  $view_modes = \Drupal::entityManager()
    ->getViewModes('node');
  foreach ($fields->fields as $field_name => $config) {
    $field_storage = FieldStorageConfig::loadByName($config['entity_type'], $field_name);
    if (empty($field_storage)) {
      FieldStorageConfig::create(array(
        'field_name' => $field_name,
        'entity_type' => $config['entity_type'],
        'type' => $config['type'],
      ))
        ->save();
    }
  }
  foreach ($fields->bundle_types as $bundle) {
    foreach ($fields->fields as $field_name => $config) {
      $config_array = array(
        'field_name' => $field_name,
        'entity_type' => $config['entity_type'],
        'bundle' => $bundle,
        'label' => $config['label'],
        'required' => $config['required'],
      );
      if (isset($config['settings'])) {
        $config_array['settings'] = $config['settings'];
      }
      $field = FieldConfig::loadByName($config['entity_type'], $bundle, $field_name);
      if (empty($field) && $bundle !== "" && !empty($bundle)) {
        FieldConfig::create($config_array)
          ->save();
      }
      if ($bundle !== "" && !empty($bundle)) {
        if (!empty($field)) {
          $field
            ->setLabel($config['label'])
            ->save();
          $field
            ->setRequired($config['required'])
            ->save();
        }
        if ($config['widget']) {
          entity_get_form_display($config['entity_type'], $bundle, 'default')
            ->setComponent($field_name, $config['widget'])
            ->save();
        }
        if ($config['formatter']) {
          foreach ($config['formatter'] as $view => $formatter) {
            if (isset($view_modes[$view]) || $view == "default") {

              //$formatter['type'] = ($formatter['type'] == "addcartsearch") ? "addtocart"  : $formatter['type'];
              entity_get_display($config['entity_type'], $bundle, $view)
                ->setComponent($field_name, !is_array($formatter) ? $config['formatter']['default'] : $config['formatter']['default'])
                ->save();
            }
          }
        }
      }
    }
  }
}