So as not to confuse developers and provide a more generic method, could we deprecate Node::getTitle() in favour of Entity::label()?

I'm not sure we need getTitle() if label() exists. Also, I can't find any documentation explaining when one should be used over the other. It seems like label() is always the way to go.

Let's do this for all entity-specific methods, not just Nodes:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

colan created an issue. See original summary.

Berdir’s picture

getTitle() was added in the original Node/EntityNG conversion on the request of @webchick because she thought that label() was confusing. That might have changed since then, it was a long time ago and generic entity API was new thing back then. You likely still want to involve here in this.

colan’s picture

Can someone who knows how to get in touch with her let her know about this issue? She's not in #drupal-contribute and her contact form is disabled. Thanks!

idebr’s picture

If/when this is greenlighted we might as well deprecate Term::getName(), since it is a similar wrapper around $this->label()

Nay sayers might argue the entity label should match the input label, so node's 'Title' matches the code Node::getTitle(). This is currently true for the content entity types in core: node, taxonomy term, media. Actually MediaInterface was recently expanded to explicitly use getName / setName in #2897009: MediaInterface is missing setName() and getName()

timmillwood’s picture

Assigned: Unassigned » webchick

Assigning to webchick

webchick’s picture

Assigned: webchick » Unassigned

Sure. I'm not gonna argue with people who actually use the subsystem every day. :)

colan’s picture

Title: Deprecate Node::getTitle() in favour of Entity::label() » Deprecate Node::getTitle() & other entity-specific calls in favour of Entity::label()
Issue summary: View changes

#6: Great, thanks!

#4: Yes, I agree. I just updated the title and IS to reflect that we want to do this for all entities, not just Nodes. Please add any others I've missed.

colan’s picture

Title: Deprecate Node::getTitle() & other entity-specific calls in favour of Entity::label() » Deprecate Node::getTitle() & other entity-type-specific calls in favour of Entity::label()
seanB’s picture

WimLeers created #2897009: MediaInterface is missing setName() and getName() while working on #2835767-46: Media + REST: comprehensive test coverage for Media + MediaType entity types. I can't find why it was a bug, but maybe he can provide some insight?

Should we also deprecate wrapper methods around bundle(), like NodeInterface::getType()?

colan’s picture

Maybe let's keep that as a separate, but related, issue so that we don't try to do too many things as once here?

andypost’s picture

This needs separate issues per entity type to clean-up usage as well but first it needs approval

andypost’s picture

Component: node system » entity system

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

colan’s picture

As I just ran into this again, and none of those folks have commented yet, I thought it best to ping them on Slack.

hchonov’s picture

I am +1 on doing the change across all entities. No need for multiple methods doing the same thing, which is actually what is confusing.

hchonov’s picture

colan’s picture

Assigned: Unassigned » colan

Thanks! Stub change record created. Working on the patch...

colan’s picture

Here's a first run at this.

colan’s picture

Assigned: colan » Unassigned
catch’s picture

+1 I think people are used to the generic entity methods, also sometimes I see something like getType() and have to remind myself it's an alias for bundle().

If this doesn't make it into 8.8.x, it could still go into 8.9.x but the deprecations will need to be for 10.0.x

Berdir’s picture

+++ b/core/modules/node/src/NodeInterface.php
@@ -56,6 +56,11 @@ public function getType();
    * @return string
    *   Title of the node.
+   *
+   * @deprecated in drupal:8.8.0, will be removed from drupal:9.0.0. Use
+   *   \Drupal\Core\Entity\Entity::label() instead
+   *
+   * @see https://www.drupal.org/node/3085319
    */
   public function getTitle();

what about getType()/bundle()?

Not sure if we split the issue/issues on entity type or type of method. Lets see how big it will be with all the replacements done in tests and so on.

Status: Needs review » Needs work

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

jungle’s picture

In addition to getType()/bundle(), what about setName($name)/set($name, $value, $notify = TRUE) ? Should it be deprecated as well? Whether do it in a separate issue?

Berdir’s picture

no, setName() and set() are not the same thing.

jungle’s picture

Re #27, let me reword it.

setName() is just an variation of setTitle().

Node::getTitle() is getting deprecated which makes me wondering should Node::setTitle() be deprecated.

Further, I am thinking of introducing a common method setLabel() to the base class ContentEntityBase.

Then setTitle(), setName() calls can be deprecated in favour of using setLabel().

Or deprecate them directly, as setTitle() is just a wrapper of the set() method. (see below). Personally, I prefer the former way -- introduce setLabel(), and deprecate setTitle(), setName()

/**
   * {@inheritdoc}
   */
  public function setTitle($title) {
    $this->set('title', $title);
    return $this;
  }

  1. +++ b/core/modules/node/src/Entity/Node.php
    @@ -197,7 +197,8 @@ public function access($operation = 'view', AccountInterface $account = NULL, $r
       public function getTitle() {
    -    return $this->get('title')->value;
    +    @trigger_error(__NAMESPACE__ . '\Node::getTitle is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use \Drupal\Core\Entity\Entity::label() instead. See https://www.drupal.org/node/3085319', E_USER_DEPRECATED);
    +    return $this->label();
       }
    

    And here, should Use \Drupal\Core\Entity\Entity::label() instead be Use \Drupal\node\Entity\Node::label() instead?

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.