Skip to content

Added getField method to FieldsHelper.php #45595

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: 6.0-dev
Choose a base branch
from

Conversation

CraftException
Copy link

@CraftException CraftException commented Jun 11, 2025

Pull Request for Issue # .

Summary of Changes

Added getField method to FieldsHelper.php to get a single field instead of every field of an item.

Testing Instructions

Actual result BEFORE applying this Pull Request

If you want to get a single field of an item, you have to foreach trough every field, until you found the correct field.

Expected result AFTER applying this Pull Request

You can now just use this function.

Link to documentations

Please select:

  • Documentation link for docs.joomla.org:

  • No documentation changes for docs.joomla.org needed

  • Pull Request link for manual.joomla.org:

  • No documentation changes for manual.joomla.org needed

@CraftException CraftException marked this pull request as draft June 11, 2025 12:34
@CraftException CraftException force-pushed the 6.0-dev branch 3 times, most recently from 95a3248 to c6f3971 Compare June 11, 2025 13:35
@CraftException CraftException marked this pull request as ready for review June 11, 2025 13:38
@CraftException CraftException force-pushed the 6.0-dev branch 2 times, most recently from 4d3a830 to 0315638 Compare June 11, 2025 13:45
@Ruud68
Copy link
Contributor

Ruud68 commented Jun 20, 2025

Is there a reason why you do not do a $this->getFields(...) and then return the field with Id if it exists?
That would save a lot of (duplicated) code in the new function?

so something like this:

public static function getField(
        $context,
        $fieldId,
        $item = null,
        $prepareValue = false,
        ?object $valueToOverride = null,
        bool $includeSubformFields = false
    ) {
        $fields = $this->getFields($context, $item, $prepareValue, $valuesToOverride, $includeSubformFields);
        $fields = array_column($fields, NULL, 'id');
        return $fields[$fieldId] ?? null;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants