Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
38 / 38
100.00% covered (success)
100.00%
2 / 2
CRAP
n/a
0 / 0
primary_entity_reference_post_update_add_show_primary_only_setting
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
1 / 1
8
primary_entity_reference_post_update_add_button_primary_label_setting
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
1 / 1
8
1<?php
2
3/**
4 * @file
5 * Post-update functions for Primary Entity Reference module.
6 */
7
8declare(strict_types=1);
9
10/**
11 * Add show_primary_only setting to existing widget configurations.
12 */
13function primary_entity_reference_post_update_add_show_primary_only_setting(&$sandbox) {
14  $config_factory = \Drupal::configFactory();
15  $updated_count  = 0;
16
17  // Widget plugin IDs to check.
18  $widget_types = [
19    'primary_entity_reference_inline_form',
20    'primary_entity_reference_options_buttons',
21    'primary_entity_reference_autocomplete',
22  ];
23
24  // Iterate through all form display configurations.
25  foreach ($config_factory->listAll('core.entity_form_display.') as $config_name) {
26    $display = $config_factory->getEditable($config_name);
27    $content = $display->get('content');
28    $changed = FALSE;
29
30    if (empty($content)) {
31      continue;
32    }
33
34    // Check each field component.
35    foreach ($content as $field_name => $component) {
36      // Check if this component uses one of our widgets.
37      if (isset($component['type']) && in_array($component['type'], $widget_types, TRUE)) {
38        // Add show_primary_only setting if it doesn't exist.
39        if (!isset($component['settings']['show_primary_only'])) {
40          $content[$field_name]['settings']['show_primary_only'] = FALSE;
41          $changed = TRUE;
42        }
43      }
44    }
45
46    // Save the configuration if changes were made.
47    if ($changed) {
48      $display->set('content', $content);
49      $display->save();
50      $updated_count++;
51    }
52  }
53
54  return t('Updated @count form display(s) with show_primary_only setting.', [
55    '@count' => $updated_count,
56  ]);
57}
58
59/**
60 * Add primary_label setting to existing buttons widget configurations.
61 */
62function primary_entity_reference_post_update_add_button_primary_label_setting(&$sandbox) {
63  $config_factory = \Drupal::configFactory();
64  $updated_count  = 0;
65
66  // Widget plugin IDs to check.
67  $widget_types = 'primary_entity_reference_options_buttons';
68
69  // Iterate through all form display configurations.
70  foreach ($config_factory->listAll('core.entity_form_display.') as $config_name) {
71    $display = $config_factory->getEditable($config_name);
72    $content = $display->get('content');
73    $changed = FALSE;
74
75    if (empty($content)) {
76      continue;
77    }
78
79    // Check each field component.
80    foreach ($content as $field_name => $component) {
81      // Check if this component uses one of our widgets.
82      if (isset($component['type']) && $component['type'] === $widget_types) {
83        // Add primary_label setting if it doesn't exist.
84        if (!isset($component['settings']['primary_label'])) {
85          $content[$field_name]['settings']['primary_label'] = 'Primary';
86          $changed = TRUE;
87        }
88      }
89    }
90
91    // Save the configuration if changes were made.
92    if ($changed) {
93      $display->set('content', $content);
94      $display->save();
95      $updated_count += 1;
96    }
97  }
98
99  return t('Updated @count form display(s) with primary label setting.', [
100    '@count' => $updated_count,
101  ]);
102}