Created
July 22, 2024 21:04
-
-
Save isalmanhaider/9c369276e6c0c787a48fbcf2513cf735 to your computer and use it in GitHub Desktop.
find content types containing fields
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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