Skip to content
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

Multiple phone numbers on one page results in only the last number being output #10

Closed
JacobDB opened this issue Nov 20, 2020 · 9 comments

Comments

@JacobDB
Copy link

JacobDB commented Nov 20, 2020

With multiple phone numbers set, it seems that only the last phone number can be output. In the example below, assuming "+13128675309" is set to $phone_number and "+13128675310" is set to $fax_number, both echos will output "+13128675310".

$phone_number = get_field("phone_number", "theme");
$fax_number   = get_field("fax_number", "theme");

echo $phone_number->national;
echo $fax_number->national;

This has only been tested when installed without composer, per #8.

Am I doing this incorrectly, or is this a bug?

@JacobDB
Copy link
Author

JacobDB commented Nov 20, 2020

Never mind! If I convert the value to an array before calling in the second field, it works. Thanks!

Example for other idiots like me:

$phone_number = get_field("phone_number", "theme");
$phone_number = $phone_number ? $phone_number->toArray() : false;
$fax_number   = get_field("fax_number", "theme");
$fax_number   = $fax_number ? $fax_number->toArray() : false;

@JacobDB JacobDB closed this as completed Nov 20, 2020
@JacobDB
Copy link
Author

JacobDB commented Nov 20, 2020

Just spent a few minutes trying to get this to work automatically with acf/format_value/type=phone_number, but that doesn't seem to be possible. I think it'd be awesome if something like that could work (even if it's a hook as part of this plugin), but my solution does work, so it's fine, just not super elegant.

@Log1x
Copy link
Owner

Log1x commented Nov 20, 2020

I'll think about a better approach to fix these problems.

@JacobDB
Copy link
Author

JacobDB commented Mar 31, 2021

@Log1x running in to this issue again, in a foreach pulling from a repeater... is there a way to fix this? I'm basically doing this:

$phone_numbers = get_field("phone_numbers", "options");

foreach ($phone_numbers as $data) {
    $phone_number = $data["phone_number"]->toArray();

    echo "<pre>";
    print_r($phone_number);
    echo "</pre>";
}

This is resulting in every every loop outputting the last value in the array... doesn't make a ton of sense to me.

EDIT: Also just tried in a for loop instead, and in a while (have_rows()) too. Always the same result. I even tried calling each one like $phone_numbers[0]["phone_number"]->toArray(), and still, same problem. It seems that this field simply will not working within a repeater, very frustrating...

@JacobDB JacobDB reopened this Mar 31, 2021
@JacobDB
Copy link
Author

JacobDB commented Mar 31, 2021

Looking through the code, I think the problem is that it's implemented as a "singleton," which to my understanding only one instance can exist on a page at once.

@JacobDB
Copy link
Author

JacobDB commented Mar 31, 2021

Made some progress, but I'm not a fan of this solution.

$phone_numbers  = get_field("phone_numbers", "theme");

/**
 * Hack to prevent the last phone number always being used
 */
use Log1x\AcfPhoneNumber\PhoneNumber;

foreach ($phone_numbers as $key => $data) {
    if ($raw = get_option("theme_phone_numbers_{$key}_phone_number")) {
        $parsed = new PhoneNumber($raw);
        $phone_numbers[$key]["phone_number"] = $parsed->toArray();
    } else {
        $phone_numbers[$key]["phone_number"] = null;
    }
}

foreach ($phone_numbers as $data) {
    echo $data["phone_number"]->e164;
}

By accessing the values outside of ACF, I can get each instance individually and convert it, rather than each instance being generated in the repeater and stored in an array (which results in only the last instance being accessible). The drawback is that this retrieves the same value from the database twice for each number, and the code is... sloppy. I feel like there's got to be a better solution, maybe if I tie in to the acf/fiels/type=repeater hook I can do this there, but I doubt it... will have to keep looking.

@Log1x
Copy link
Owner

Log1x commented Mar 31, 2021

are you on the latest version? I didn't think this should be an issue anymore since #17

@JacobDB
Copy link
Author

JacobDB commented Mar 31, 2021

I thought I was, but I'll double check and report back.

@JacobDB
Copy link
Author

JacobDB commented Mar 31, 2021

Yep, you were right 🤦‍♂️ Thanks, I should've checked that a lot sooner, my bad. It's been a long week 😅

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

No branches or pull requests

2 participants