Skip to content

Instantly share code, notes, and snippets.

@isalmanhaider
Created July 22, 2024 21:04
Show Gist options
  • Save isalmanhaider/9c369276e6c0c787a48fbcf2513cf735 to your computer and use it in GitHub Desktop.
Save isalmanhaider/9c369276e6c0c787a48fbcf2513cf735 to your computer and use it in GitHub Desktop.
find content types containing fields
<?php
use Drupal\Core\Database\Database;
use Drush\Drush;
function find_content_types_with_fields() {
$fields_to_find = [
'field_salesforce_account_id',
'field_publish_on_csrwire',
];
$database = Database::getConnection();
foreach ($fields_to_find as $field_name) {
$query = $database->select('config', 'c')
->fields('c', ['name', 'data'])
->condition('c.name', 'field.field.node.%' . $field_name, 'LIKE');
$result = $query->execute()->fetchAll();
foreach ($result as $record) {
$data = unserialize($record->data);
if (isset($data['entity_type']) && $data['entity_type'] == 'node') {
$bundle = str_replace('field.field.node.', '', $record->name);
$bundle = str_replace('.' . $field_name, '', $bundle);
Drush::output()->writeln("Content type '$bundle' has the field '{$data['field_name']}'.");
}
}
}
}
find_content_types_with_fields();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment