View source
<?php
namespace Drupal\entity_update;
class EntityCheck {
public static function getEntityTypesList($type = NULL, $print = TRUE) {
$itmes_list = \Drupal::entityTypeManager()
->getDefinitions();
$table = [
'#theme' => 'table',
'#cache' => [
'max-age' => 0,
],
'#caption' => 'Entity types list',
'#header' => [
'Type ID',
'Type',
'Name',
],
'#rows' => [],
];
foreach ($itmes_list as $itme_name => $item_object) {
if ($type && strstr($itme_name, $type) === FALSE) {
continue;
}
$table['#rows'][] = [
$itme_name,
$item_object
->getGroup(),
$item_object
->getLabel(),
];
}
if ($print) {
EntityUpdatePrint::drushPrintTable($table);
}
return $table;
}
public static function getEntityList($type, $start = 0, $length = 10, $print = TRUE) {
$start = (int) $start;
$length = (int) $length;
$entities = [];
try {
$query = \Drupal::entityQuery($type);
if ($length) {
$query
->range($start, $length);
}
$ids = $query
->execute();
$entities = \Drupal::entityTypeManager()
->getStorage($type)
->loadMultiple($ids);
} catch (\Exception $ex) {
EntityUpdatePrint::drushLog($ex
->getMessage(), 'error');
return NULL;
}
$table = [
'#theme' => 'table',
'#caption' => 'List of the entities : ' . $type,
'#header' => [
'ID',
'Label',
],
'#rows' => [],
];
foreach ($entities as $id => $entiy) {
$table['#rows'][] = [
$id,
$entiy
->label(),
];
}
if ($print) {
EntityUpdatePrint::drushPrintTable($table);
}
return $table;
}
public static function showEntityStatusCli() {
$flg_updatable = TRUE;
$esp = " ";
$arr = "-> ";
$list = EntityUpdate::getEntityTypesToUpdate();
if (empty($list)) {
EntityUpdatePrint::drushPrint(' -> ALl Entities are up to date');
}
else {
foreach ($list as $item => $entity_type_changes) {
EntityUpdatePrint::drushPrint(" -> {$item} . Change(s) : " . count($entity_type_changes));
$flg_has_install = FALSE;
$flg_has_uninstall = FALSE;
$flg_has_update = FALSE;
foreach ($entity_type_changes as $entity_change_summ) {
EntityUpdatePrint::drushPrint($esp . strip_tags($entity_change_summ));
if (strstr($entity_change_summ, "updated")) {
$flg_has_update = TRUE;
}
elseif (strstr($entity_change_summ, "uninstalled")) {
$flg_has_uninstall = TRUE;
}
else {
$flg_has_install = TRUE;
}
}
if ($flg_has_update || $flg_has_install && $flg_has_uninstall) {
if (empty(\Drupal::entityQuery($item)
->execute())) {
EntityUpdatePrint::drushLog("{$esp}{$arr}" . "Entity type '{$item}' is updatable.", 'ok');
EntityUpdatePrint::drushPrint("{$esp}{$arr}" . "Use: drush upe --basic");
}
else {
EntityUpdatePrint::drushLog("{$esp}{$arr}" . "Multiple actions detected on '{$item}'.", 'warning');
EntityUpdatePrint::drushLog("{$esp}{$arr}" . "The entity '{$item}' is not empty", 'warning');
EntityUpdatePrint::drushPrint("{$esp}{$arr}" . "Try: drush upe --basic --force");
EntityUpdatePrint::drushPrint("{$esp}{$arr}" . "Refer to the documentation");
}
$flg_updatable = FALSE;
}
else {
EntityUpdatePrint::drushLog("{$esp}{$arr}" . "Entity type '{$item}' is updatable.", 'ok');
EntityUpdatePrint::drushPrint("{$esp}{$arr}" . "Use: drush upe {$item}");
}
EntityUpdatePrint::drushPrint("");
}
}
return $flg_updatable;
}
}