convert_bundles.module in Convert Bundles 8
Contains convert_bundles.module..
File
convert_bundles.moduleView source
<?php
/**
* @file
* Contains convert_bundles.module..
*/
use Drupal\system\Entity\Action;
use Drupal\Core\Database\Database;
use Drupal\Core\Entity\EntityInterface;
/**
* Implements hook_entity_operation_alter().
*/
function convert_bundles_entity_operation_alter(array &$operations, EntityInterface $entity) {
// TODO: tried to do this as suggested at https://www.drupal.org/node/2020549
// with // Loading all configured actions for the comment entity type.
// $actions = entity_load_multiple_by_properties('action')
// but caused an oom error.
// so we load the config table and do this manually instead.
// terrible.
// TODO: a hook to utilize on entity type creation is not obvious.
// tried hook_entity_type_build but got oom errors.
// This seems to work, but doesnt feel right.
$convert_bundles_config = 'system.action.convert_bundles_on_';
$db = Database::getConnection();
$query = $db
->select('config')
->fields('config', [
'name',
])
->condition('config.name', "%" . $db
->escapeLike($convert_bundles_config) . "%", 'LIKE');
$existing_config = $query
->execute()
->fetchAll(\PDO::FETCH_COLUMN);
foreach (\Drupal::entityTypeManager()
->getDefinitions() as $entity_type_machine_name => $entity_type) {
if (!in_array($convert_bundles_config . $entity_type_machine_name, $existing_config)) {
_create_action($entity_type_machine_name, $entity_type
->getLabel());
}
}
}
/**
* Create Action.
*/
function _create_action($entity_type_machine_name, $entity_type_label) {
$label = 'Convert ' . $entity_type_label . ' Entity Bundles';
// Creating a new configured action.
$action = Action::create([
'id' => 'convert_bundles_on_' . $entity_type_machine_name,
'label' => $label,
'type' => $entity_type_machine_name,
'configuration' => [],
'plugin' => 'convert_bundles_action_base',
]);
$action
->save();
\Drupal::messenger()
->addStatus(t('Action @label created from convert_bundles module.', [
'@label' => $label,
]));
}
Functions
Name | Description |
---|---|
convert_bundles_entity_operation_alter | Implements hook_entity_operation_alter(). |
_create_action | Create Action. |