Problem/Motivation

According to #2346261: Deprecate entity_create() in favor of a <EntityType>::create($values) or \Drupal::entityManager()->getStorage($entity_type)->create($values), entity_create() function is going to be deprecated so we shouldn't use it anymore. When the entity type is known we should directly call <EntityType>::create(). What to do when the entity type is not known or is variable is upon discussions.

Beta phase evaluation

Reference: https://www.drupal.org/core/beta-changes
Issue category Task
Issue priority Normal because it's just about code cleanup and good practices
Prioritized changes The main goal of this issue is DX, performance and removing code already deprecated for 8.0.0. (Direct calls to EntityType::create are better than generic calls to entity_create for readability)
Disruption This change is not disruptive at all as it only replaces deprecated functions call by their exact equivalent.

Proposed resolution

Replace the deprecated call to entity_create() by a proper call to <EntityType>::create().

Before:

entity_create('field_config', $field_values)->save();

After:

use Drupal\field\Entity\FieldConfig;
FieldConfig::create($field_values)->save();

Remaining tasks

Contributor tasks needed
Task Novice task? Contributor instructions Complete?
Create a patch Instructions Done
Manually test the patch Novice Instructions
Review patch to ensure that it fixes the issue, stays within scope, is properly documented, and follows coding standards Instructions

User interface changes

None.

API changes

None.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Mac_Weber created an issue. See original summary.

Mac_Weber’s picture

Issue summary: View changes
Status: Active » Needs review
FileSize
1.36 KB
mpdonadio’s picture

Status: Needs review » Needs work

Thanks for the patch.

  1. From d7b10e42dd9f8f32cfa4cc248d924cc0b0d8a01a Mon Sep 17 00:00:00 2001
    From: Mac_Weber <Mac_Weber@789986.no-reply.drupal.org>
    Date: Mon, 28 Dec 2015 16:54:16 -0200
    Subject: [PATCH] Issue #2641522 by Mac_Weber: Replace deprecated usage of
     entity_create('date_format') with a direct call to DateFormat::create()
     
    

    Normally, we just do a `git diff origin/8.1.x` instead of `git patch` when working with core patches.

  2. +++ b/core/modules/system/src/Tests/System/DateTimeTest.php
    @@ -9,6 +9,7 @@
     use Drupal\Core\Url;
     use Drupal\simpletest\WebTestBase;
    +use Drupal\Core\Datetime\Entity\DateFormat;
     
    

    Should be in alphabetical order.

Otherwise, I think this is fine. This is the only usage, and it is in a test already (and it still passes), so I don't think we need more test coverage. Per the allowed changes during the Drupal 8 release cycle, this doesn't qualify for patch release, but 8.1.x is OK.

Setting NW for the nit in the `use` section.

Mac_Weber’s picture

Status: Needs work » Needs review

@mpdonadio regarding item 2 once I had pointed the same mistake made by another developer, but other people said it does not need to be in alphabetical order. On that time I'd looked for the coding standard about this rule, but I could not find anything. Please post a link for this rule if it really exists so I could also point people in the other issue.

regarding item 1 I got used to always use git-format =P

mpdonadio’s picture

Status: Needs review » Needs work

@Mac_Weber, ordering the `use` section isn't explicitly in the coding standards right now and I can't raise anyone on IRC right now. I have had core patches pushed back for this before, and every single core file that I have examined has them ordered. Speaking as the maintainer of this subsystem, I would like to keep this usage (get it? :) for all patches that get committed. I did ping one of the doc maintainers to see if she recalls a discussion about this.

Mac_Weber’s picture

Status: Needs work » Needs review
FileSize
1.32 KB
560 bytes
mpdonadio’s picture

Status: Needs review » Reviewed & tested by the community

Assuming this comes back green...

catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed/pushed to 8.1.x, thanks!

  • catch committed d388609 on 8.1.x
    Issue #2641522 by Mac_Weber: Replace deprecated usage of  entity_create...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.