Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / user / src / Plugin / migrate / source / ProfileField.php
1 <?php
2
3 namespace Drupal\user\Plugin\migrate\source;
4
5 use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase;
6 use Drupal\migrate\Row;
7
8 /**
9  * Profile field source from database.
10  *
11  * @MigrateSource(
12  *   id = "profile_field",
13  *   source_module = "profile"
14  * )
15  */
16 class ProfileField extends DrupalSqlBase {
17
18   /**
19    * The source table containing profile field info.
20    *
21    * @var string
22    */
23   protected $fieldTable;
24
25   /**
26    * The source table containing the profile values.
27    *
28    * @var string
29    */
30   protected $valueTable;
31
32   /**
33    * {@inheritdoc}
34    */
35   public function query() {
36     if (empty($this->fieldTable) || empty($this->valueTable)) {
37       if ($this->getModuleSchemaVersion('system') >= 7000) {
38         $this->fieldTable = 'profile_field';
39         $this->valueTable = 'profile_value';
40       }
41       else {
42         $this->fieldTable = 'profile_fields';
43         $this->valueTable = 'profile_values';
44       }
45     }
46     return $this->select($this->fieldTable, 'pf')->fields('pf');
47   }
48
49   /**
50    * {@inheritdoc}
51    */
52   public function prepareRow(Row $row) {
53     if ($row->getSourceProperty('type') == 'selection') {
54       // Get the current options.
55       $current_options = preg_split("/[\r\n]+/", $row->getSourceProperty('options'));
56       // Select the list values from the profile_values table to ensure we get
57       // them all since they can get out of sync with profile_fields.
58       $options = $this->select($this->valueTable, 'pv')
59         ->distinct()
60         ->fields('pv', ['value'])
61         ->condition('fid', $row->getSourceProperty('fid'))
62         ->execute()
63         ->fetchCol();
64       $options = array_merge($current_options, $options);
65       // array_combine() takes care of any duplicates options.
66       $row->setSourceProperty('options', array_combine($options, $options));
67     }
68
69     if ($row->getSourceProperty('type') == 'checkbox') {
70       // D6 profile checkboxes values are always 0 or 1 (with no labels), so we
71       // need to create two label-less options that will get 0 and 1 for their
72       // keys.
73       $row->setSourceProperty('options', [NULL, NULL]);
74     }
75
76     return parent::prepareRow($row);
77   }
78
79   /**
80    * {@inheritdoc}
81    */
82   public function fields() {
83     return [
84       'fid' => $this->t('Primary Key: Unique profile field ID.'),
85       'title' => $this->t('Title of the field shown to the end user.'),
86       'name' => $this->t('Internal name of the field used in the form HTML and URLs.'),
87       'explanation' => $this->t('Explanation of the field to end users.'),
88       'category' => $this->t('Profile category that the field will be grouped under.'),
89       'page' => $this->t("Title of page used for browsing by the field's value"),
90       'type' => $this->t('Type of form field.'),
91       'weight' => $this->t('Weight of field in relation to other profile fields.'),
92       'required' => $this->t('Whether the user is required to enter a value. (0 = no, 1 = yes)'),
93       'register' => $this->t('Whether the field is visible in the user registration form. (1 = yes, 0 = no)'),
94       'visibility' => $this->t('The level of visibility for the field. (0 = hidden, 1 = private, 2 = public on profile but not member list pages, 3 = public on profile and list pages)'),
95       'autocomplete' => $this->t('Whether form auto-completion is enabled. (0 = disabled, 1 = enabled)'),
96       'options' => $this->t('List of options to be used in a list selection field.'),
97     ];
98   }
99
100   /**
101    * {@inheritdoc}
102    */
103   public function getIds() {
104     $ids['fid']['type'] = 'integer';
105     return $ids;
106   }
107
108 }