我正在尝试遍历JSON迁移源中的数组(称为“属性”),但我不知道如何实现。下面是我的数据源的一个简短示例:
[
{
"Id": "ST00000002",
"Title": "Fast & Furious 9",
"Attributes": [
"0000000013",
"0000000015"
]
}
]我的最终目标是根据这些属性值检查某些复选框/布尔字段,但我不知道如何单独处理每个属性。
到目前为止我尝试过的事情:
single_value和multiple_valuesDeepen进程插件(如本期上所示)sub_processstatic_map插件(可以这样做):map:
'0000000015': 1
default_value: 0skip_on_value与not_equals: true的结合我觉得这不可能像我所做的那样困难,所以我希望有人能帮助我。非常感谢。
发布于 2020-06-02 13:27:40
非常感谢。我使用"in_array“创建了一个定制的进程插件,就像建议的那样。如果有人感兴趣,下面是我创建的插件:
namespace Drupal\migrate_veezi\Plugin\migrate\process;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\MigrateException;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\Row;
/**
* Check if value exists in array
*
* @MigrateProcessPlugin(
* id = "in_array"
* )
*
* To check if value is in array, use the following:
*
* @code
* field_text:
* plugin: in_array
* source: array
* search: 'string'
* @endcode
*
*/
class InArray extends ProcessPluginBase {
/**
* {@inheritdoc}
*/
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
$search = $this->configuration['search'];
if ($search) {
if (is_array($value) || $value instanceof \Traversable) {
return in_array($search, $value);
}
else {
throw new MigrateException(sprintf('%s is not traversable', var_export($value, TRUE)));
}
}
else {
throw new MigrateException(sprintf('search value is not set'));
}
}
}然后我就这样用了它:
field_closed_caption:
-
plugin: single_value
source: attributes
-
plugin: in_array
search: '0000000007'https://drupal.stackexchange.com/questions/294165
复制相似问题