You are here

function http_response_headers_update_8001 in HTTP Response Headers 2.0.x

Same name and namespace in other branches
  1. 8.2 http_response_headers.install \http_response_headers_update_8001()

Changing to using entities instead of configuration objects.

File

./http_response_headers.install, line 47
HTTP Header manipulations install.

Code

function http_response_headers_update_8001() {
  $old_config = \Drupal::configFactory()
    ->get('http_response_headers.settings');
  if (!empty($old_config
    ->get('headers'))) {
    foreach ($old_config
      ->get('headers') as $header) {
      $entity = array(
        'id' => preg_replace('/[^a-z0-9_]+/', '_', strtolower($header['name'])),
        'label' => $header['name'],
        'name' => $header['name'],
        'description' => '',
        'group' => $header['group'],
        'value' => $header['value'],
      );
      $entity_manager = \Drupal::entityTypeManager()
        ->getStorage('response_header');
      if ($existing_entity = current($entity_manager
        ->loadByProperties(array(
        'name' => $entity['name'],
      )))) {
        $saved = $existing_entity
          ->set('value', $entity['value'])
          ->save();
      }
      else {
        $saved = $entity_manager
          ->create($entity)
          ->save();
      }
    }
    \Drupal::configFactory()
      ->getEditable('http_response_headers.settings')
      ->delete();
  }
}