You are here

function flag_lists_update_8002 in Flag Lists 8

Same name and namespace in other branches
  1. 4.0.x flag_lists.install \flag_lists_update_8002()

Update all views to use the updated Flag List Item table info.

File

./flag_lists.install, line 66
Contains install and updates for Flag Lists module.

Code

function flag_lists_update_8002() {
  $config_factory = \Drupal::configFactory();

  // Find all views configs.
  foreach ($config_factory
    ->listAll('views.view.') as $view_config_name) {
    $view = $config_factory
      ->getEditable($view_config_name);

    // Go through each display on each view.
    $displays = $view
      ->get('display');
    foreach ($displays as $display_name => $display) {

      // Go through all the entity fields on each display and
      // find ones currently using 'bundle' as the table.
      if (!empty($display['display_options']['fields'])) {
        foreach ($display['display_options']['fields'] as $field_name => $field) {
          if (isset($field['entity_type']) && $field['entity_type'] === 'flag_list_item' && $field['entity_field'] === 'bundle') {
            $base = "display.{$display_name}.display_options.fields.{$field_name}";

            // Update the fields to use the updated table.
            $view
              ->set($base . '.id', 'type');
            $view
              ->set($base . '.field', 'type');
            $view
              ->set($base . '.label', 'Entity Type');
            $view
              ->set($base . '.entity_field', 'type');
            $type = $view
              ->get($base);
            $view
              ->clear($base);
            $base = "display.{$display_name}.display_options.fields.type";
            $view
              ->set($base, $type);
          }
        }
      }
    }
    $view
      ->save(TRUE);
  }
  $message = 'Update all views to use updated Flag List Items. Please review the My Flag Lists Items view as it has been tweaked.';
  return $message;
}