Skip to content

Commit

Permalink
Allow field definition in yml and annotation
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Borchert <[email protected]>
  • Loading branch information
Stefan Borchert committed Jul 3, 2017
1 parent bade654 commit bec65fb
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 11 deletions.
44 changes: 38 additions & 6 deletions src/Plugin/migrate/source/SqlBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,7 @@ public function __construct(array $configuration, $plugin_id, $plugin_definition
// Now we can safely call the parent constructor.
parent::__construct($configuration, $plugin_id, $plugin_definition, $migration, $state);

if (($this->table = $this->getConfig('table')) === NULL || empty($this->table['name'])) {
// No table defined as source for user data.
throw new MigrateException('No source table defined. Set this in the migration yml file or in the source plugin.');
}
$this->prepareTableInfo();
}

/**
Expand All @@ -71,6 +68,10 @@ public function fields() {
// Define base fields for user accounts.
$fields = [];

foreach ($this->getTableFields() as $field => $label) {
$fields[$field] = $label;
}

// Add required fields from extending classes.
$this->alterFields($fields);

Expand Down Expand Up @@ -110,7 +111,7 @@ protected function alterQuery(SelectInterface $query) {
* @param array $fields
* List of fields available for migration.
*/
protected function alterFields(array $fields = []) {
protected function alterFields(array &$fields = []) {
// Give extending classes the possibility to alter or add fields.
}

Expand Down Expand Up @@ -185,6 +186,27 @@ protected function databaseConfigAddDefaults(array $config) {
];
}

/**
* Prepare information of base table.
*
* @throws \Drupal\migrate\MigrateException
*/
protected function prepareTableInfo() {
if (($this->table = $this->getConfig('table')) === NULL || empty($this->table['name'])) {
// No table defined as source for user data.
throw new MigrateException('No source table defined. Set this in the migration yml file or in the source plugin.');
}
if (!isset($this->table['ids']) || !is_array($this->table['ids'])) {
$this->table['ids'] = [];
}
if (!isset($this->table['fields']) || !is_array($this->table['fields'])) {
$this->table['fields'] = [];
}
if (empty($this->table['alias'])) {
$this->table['alias'] = $this->table['name'];
}
}

/**
* Get the base table name for the migration query.
*
Expand All @@ -202,7 +224,17 @@ protected function getTableName() {
* Alias of base table.
*/
protected function getTableAlias() {
return empty($this->table['alias']) ? $this->table['name'] : $this->table['alias'];
return $this->table['alias'];
}

/**
* Get fields defined in migration yml or source plugin.
*
* @return array
* List of field labels keyed by field name.
*/
protected function getTableFields() {
return $this->table['fields'];
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ source:
alias: u
ids:
uid: integer
fields:
uid: 'User ID'
name: 'Username'
pass: 'Password'
mail: 'Email address'
track_changes: true
constants:
langcode: 'de'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,8 @@ protected function alterQuery(SelectInterface $query) {
/**
* {@inheritdoc}
*/
protected function alterFields(array $fields = []) {
$fields['uid'] = $this->t('User ID');
$fields['name'] = $this->t('Username');
$fields['pass'] = $this->t('Password');
$fields['mail'] = $this->t('Email address');
protected function alterFields(array &$fields = []) {
// Add fields not defined in yml-file.
$fields['signature'] = $this->t('Signature');
$fields['signature_format'] = $this->t('Signature format');
$fields['created'] = $this->t('Registered timestamp');
Expand Down

0 comments on commit bec65fb

Please sign in to comment.