You are here

function convert_bundles_entity_operation_alter in Convert Bundles 8

Implements hook_entity_operation_alter().

File

./convert_bundles.module, line 15
Contains convert_bundles.module..

Code

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());
    }
  }
}