Commit 55c2796a authored by Neslee Canil Pinto's avatar Neslee Canil Pinto Committed by moshe weitzman

Issue #3044234 by mikelutz, Neslee Canil Pinto: Replace deprecated entity_*...

Issue #3044234 by mikelutz, Neslee Canil Pinto: Replace deprecated entity_* calls with proper calls to the entity api
parent 4eefb59e
......@@ -121,7 +121,7 @@ function devel_generate_add_comments(NodeInterface $node, FieldDefinitionInterfa
if ($parents) {
$stub['pid'] = current($parents);
}
$comment = entity_create('comment', $stub);
$comment = \Drupal::entityTypeManager()->getStorage('comment')->create($stub);
//Populate all core fields on behalf of field.module
DevelGenerateBase::populateFields($comment);
......
......@@ -28,6 +28,13 @@ abstract class DevelGenerateBase extends PluginBase implements DevelGenerateBase
*/
protected $random;
/**
* The entity type manager service.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* {@inheritdoc}
*/
......@@ -91,13 +98,18 @@ abstract class DevelGenerateBase extends PluginBase implements DevelGenerateBase
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity to be enriched with sample field values.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
public static function populateFields(EntityInterface $entity) {
/* @var \Drupal\field\FieldConfigInterface[] $instances */
$instances = entity_load_multiple_by_properties('field_config',[
$properties = [
'entity_type' => $entity->getEntityType()->id(),
'bundle' => $entity->bundle()
]);
];
$field_config_storage = \Drupal::entityTypeManager()->getStorage('field_config');
/* @var \Drupal\field\FieldConfigInterface[] $instances */
$instances = $field_config_storage->loadByProperties($properties);
if ($skips = function_exists('drush_get_option') ? drush_get_option('skip-fields', '') : @$_REQUEST['skip-fields']) {
foreach (explode(',', $skips) as $skip) {
......@@ -158,6 +170,20 @@ abstract class DevelGenerateBase extends PluginBase implements DevelGenerateBase
return TRUE;
}
/**
* Gets the entity type manager service.
*
* @return \Drupal\Core\Entity\EntityTypeManagerInterface
* The entity type manager service.
*/
protected function getEntityTypeManager() {
if (!$this->entityTypeManager) {
$this->entityTypeManager = \Drupal::entityTypeManager();
}
return $this->entityTypeManager;
}
/**
* Returns the random data generator.
*
......
......@@ -66,7 +66,7 @@ class ExampleDevelGenerate extends DevelGenerateBase {
$account = user_load_by_name('example_devel_generate');
if (!$account) {
$account = entity_create('user', $edit);
$account = $this->getEntityTypeManager()->getStorage('user')->create($edit);
}
// Populate all fields with sample values.
......
......@@ -22,6 +22,7 @@ trait DevelGenerateSetupTrait
protected $vocabulary;
public function setUpData() {
$entity_type_manager = $this->container->get('entity_type.manager');
// Create Basic page and Article node types.
if ($this->profile != 'standard') {
$this->drupalCreateContentType(array('type' => 'page', 'name' => 'Basic Page'));
......@@ -50,13 +51,15 @@ trait DevelGenerateSetupTrait
);
$this->createEntityReferenceField('node', 'article', $field_name, NULL, 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
entity_get_form_display('node', 'article', 'default')
$entity_type_manager->getStorage('entity_form_display')
->load('node.article.default')
->setComponent($field_name, array(
'type' => 'options_select',
))
->save();
entity_get_display('node', 'article', 'default')
$entity_type_manager->getStorage('entity_view_display')
->load('node.article.default')
->setComponent($field_name, array(
'type' => 'entity_reference_label',
))
......
......@@ -29,29 +29,30 @@ class DevelControllerTest extends BrowserTestBase {
*/
protected function setUp() {
parent::setUp();
$entity_type_manager = $this->container->get('entity_type.manager');
// Create a test entity.
$random_label = $this->randomMachineName();
$data = ['type' => 'entity_test', 'name' => $random_label];
$this->entity = entity_create('entity_test', $data);
$this->entity = $entity_type_manager->getStorage('entity_test')->create($data);
$this->entity->save();
// Create a test entity with only canonical route.
$random_label = $this->randomMachineName();
$data = ['type' => 'devel_entity_test_canonical', 'name' => $random_label];
$this->entity_canonical = entity_create('devel_entity_test_canonical', $data);
$this->entity_canonical = $entity_type_manager->getStorage('devel_entity_test_canonical')->create($data);
$this->entity_canonical->save();
// Create a test entity with only edit route.
$random_label = $this->randomMachineName();
$data = ['type' => 'devel_entity_test_edit', 'name' => $random_label];
$this->entity_edit = entity_create('devel_entity_test_edit', $data);
$this->entity_edit = $entity_type_manager->getStorage('devel_entity_test_edit')->create($data);
$this->entity_edit->save();
// Create a test entity with no routes.
$random_label = $this->randomMachineName();
$data = ['type' => 'devel_entity_test_no_links', 'name' => $random_label];
$this->entity_no_links = entity_create('devel_entity_test_no_links', $data);
$this->entity_no_links = $entity_type_manager->getStorage('devel_entity_test_no_links')->create($data);
$this->entity_no_links->save();
$this->drupalPlaceBlock('local_tasks_block');
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment