TablesInterface::addField() doesn't document that $field can contain relationships

Created on 2 July 2024, 6 months ago
Updated 8 July 2024, 6 months ago

Problem/Motivation

The docs say:

   * @param string $field
   *   If it doesn't contain a dot, then an entity base field name. If it
   *   contains a dot, then either field name dot field column or field name dot
   *   delta dot field column. Delta can be a numeric value or a "%delta" for
   *   any value.

But you can chain through relationships, as seen in the test EntityQueryRelationshipTest:

      ->condition("user_id.entity.name", $this->accounts[0]->getAccountName(), '<>')

      ->condition("user_id.entity:user.name", $this->accounts[0]->getAccountName())

Steps to reproduce

Proposed resolution

Rather than document this fully here, we should refer to the comprehensive docs at Drupal\Core\Entity\Query\QueryInterface::condition()

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

๐Ÿ› Bug report
Status

Active

Version

11.0 ๐Ÿ”ฅ

Component
Documentationย  โ†’

Last updated 1 day ago

No maintainer
Created by

๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom joachim

Live updates comments and jobs are added and updated live.
  • Novice

    It would make a good project for someone who is new to the Drupal contribution process. It's preferred over Newbie.

Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @joachim
  • First commit to issue fork.
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia nishtha.pradhan

    nishtha.pradhan โ†’ made their first commit to this issueโ€™s fork.

  • Pipeline finished with Failed
    6 months ago
    Total: 181s
    #218809
  • Pipeline finished with Canceled
    6 months ago
    Total: 295s
    #218820
  • Pipeline finished with Success
    6 months ago
    Total: 513s
    #218826
  • Status changed to Needs review 6 months ago
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia vinmayiswamy

    Thank you @nishtha.pradhan, for your MR and for addressing the documentation for TablesInterface::addField() in Drupal core.

    I've reviewed the changes provided in Drupal version 11.x. To enhance clarity, I suggest explicitly stating in the documentation that dot notation (.) can be used to specify fields involving relationships. This is consistent with practices observed in tests such as EntityQueryRelationshipTest.

    For example:

    /**
     * Adds a field to a database query.
     *
     * @param string $field
     *   The field to add to the query. Use dot notation to specify fields from
     *   related entities, e.g., "entity.field" or "entity:referenced_entity.field".
     *   Delta can be a numeric value or a "%delta" for any value.
     *   
     *   See {@link https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Entity!Query!QueryInterface.php/function/QueryInterface%3A%3Acondition/8.9.x QueryInterface::condition()} for more details.
     * @param string $type
     *   Join type, can either be INNER or LEFT.
     * @param string $langcode
     *   The language code to use for multilingual queries.
     *
     * @return string
     *   The return value is a string containing the alias of the table, a dot
     *   and the appropriate SQL column as passed in. This allows the direct use
     *   of this in a query for a condition or sort.
     *
     * @throws \Drupal\Core\Entity\Query\QueryException
     *   If $field specifies an invalid relationship.
     */
    public function addField($field, $type = 'INNER', $langcode = NULL);
    

    I would appreciate feedback from others on this proposed change. Does this approach align with the understanding of how fields should be specified in Drupal queries? Are there any additional considerations we should take into account?

    Your thoughts and suggestions would be valuable.

    Thank you!

  • Status changed to Needs work 5 months ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    Seems the updates were slightly copied and paste from the summary, but don't think we should be introducing new phrase that don't already exist.

    For comprehensive documentation on the

    is not found in core. Example

    * See the @link themeable Default theme implementations topic @endlink for
    * details.

    That's just a suggestion, that may fit but should checkout other examples in the repo so we can stay more inline.

    Nitpicky but good exercise, leaving the novice tag.

  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom joachim

    The @link syntax is for linking a topic. I'm not sure it'll work with a method name.

    Just giving the full class name and method will make a link on api.d.org.

  • ๐Ÿ‡ช๐Ÿ‡ธSpain rodrigoaguilera Barcelona

    This requires to change from the @link syntax to just the class name + method

  • ๐Ÿ‡ต๐Ÿ‡ฐPakistan sadamafridi

    I'm working on this issue at DrupalCon Barcelona 2024

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States dksdev01 New York

    Reviewed and tested, looks good. thanks

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States devjuarez

    I've reviewed the changes and the link has been updated from the @link syntax to just the class name + method. Looks good.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States xjm

    Thank you everyone for working on this issue!

    We reviewed this issue LIVE at DrupalCon Barcelona 2024 at the mentored contribution sprint.

    • After reviewing the proposal, I think @joachim's proposal is a good one. I looked up the detailed documentation for QueryInterface::condition(), and it does indeed explain the needed information much more clearly and in greater detail.

    • I also looked at @vinmayiswamy's suggestion in #6. Thanks for thinking about this question! In this case, I think the linked QueryInterface::condition() documentation also sufficiently covers that information.

    • Finally, I considered @smustgrave's suggestion in #7:

      Seems the updates were slightly copied and paste from the summary, but don't think we should be introducing new phrase that don't already exist.

      For comprehensive documentation on the

      is not found in core. Example

      * See the @link themeable Default theme implementations topic @endlink for
      * details.

      That's just a suggestion, that may fit but should checkout other examples in the repo so we can stay more inline.

      This is also a good line of thinking! In this particular case, though, I think we should keep the wording that's in the current merge request. It's always helpful to have information for someone about why they might want to follow a link, so that they can decide whether or not it is helpful for them to view it.

    Adding credit for @bibliophileaxe who helped mentor the contributors who worked on this issue today.

    • xjm โ†’ committed 9d09dbc0 on 11.x
      Issue #3458565 by nishtha.pradhan, sadamafridi, joachim, smustgrave,...
    • xjm โ†’ committed 65eb6d6b on 11.0.x
      Issue #3458565 by nishtha.pradhan, sadamafridi, joachim, smustgrave,...
    • xjm โ†’ committed f3d64167 on 10.4.x
      Issue #3458565 by nishtha.pradhan, sadamafridi, joachim, smustgrave,...
    • xjm โ†’ committed 3fd22f7e on 10.3.x
      Issue #3458565 by nishtha.pradhan, sadamafridi, joachim, smustgrave,...
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States xjm

    Committed to 11.x. I've also backported the issue to 11.0.x, 10.4.x, and 10.3.x, because API documentation improvements are allowed in production bugfix (patch) releases of core.

    Thanks everyone! ๐ŸฆŽ๐ŸŒŠ๐ŸŽ‰

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States xjm

    Re-crediting myself since the System Message ate it. :)

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

Production build 0.71.5 2024