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

Get unique html element id from record view helper #2999

Merged
merged 32 commits into from
Oct 21, 2024

Conversation

RLangeUni
Copy link
Contributor

@RLangeUni RLangeUni commented Jul 21, 2023

According to #2874 (comment), this PR is the foundation for using unique ids to reference to from other tags with "aria-describedby" or similar.

$idPrefix could be the modal or the context ('list-entry', 'holds', 'illrequests', 'storage-retrieval-requests', 'search-result'). Perhaps a second parameter in getUniqueHtmlElementId could be added to differentiate between context and prefix?

TODO:

@demiankatz demiankatz added this to the 9.1 milestone Jul 24, 2023
@demiankatz demiankatz changed the title get unique html element id from record view helper Get unique html element id from record view helper Jul 24, 2023
Copy link
Member

@demiankatz demiankatz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for getting this started, @RLangeUni -- I think this has turned up some issues that require a little more work. See comments below!

@@ -415,8 +415,7 @@ public function getSearchResult($view)
*/
public function getCheckbox($idPrefix = '', $formAttr = false, $number = null)
{
$id = $this->driver->getSourceIdentifier() . '|'
. $this->driver->getUniqueId();
$id = $this->getUniqueHtmlElementId($idPrefix);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is working correctly, either in the existing dev code or in this PR. The $id value is the record identifier intended to be used as the VALUE of the checkbox. The $idPrefix value is a prefix for the HTML ID of the checkbox, but currently, the record/checkbox.phtml template does not set any HTML IDs at all or make use of the 'prefix' value in the context! We definitely do NOT want to prefix the $id value, since that is a reference to a specific record in VuFind, and prefixing it will break functionality related to it. We probably DO want to add an HTML element ID, but we'll need to adjust the template to accept it in that case. I've gone all the way back through history, and this has NEVER been set up correctly. The $idPrefix variable has been present in the helper and unused in the template since the bootstrap3/bootprint3 themes were introduced in 2014. Maybe the solution is something like this:

$context = compact('number') + [
    'id' => $this->getUniqueIdWithSourcePrefix(),
    'checkboxElementId' => $this->getUniqueHtmlElementId($idPrefix),
];

@demiankatz
Copy link
Member

@elsenhans and @RLangeUni, there hasn't been further activity here for more than a month -- just wanted to be sure you saw my earlier review. Please let me know if you need any more action/information from my end!

@elsenhans
Copy link
Contributor

Hi, @demiankatz , thanks for the reminder!
As far as I know, @RLangeUni had waited for the merge of #2982

@demiankatz
Copy link
Member

Thanks, @elsenhans -- sorry that #2982 took so long to resolve, but now that it's finally done, hopefully we can make more progress here.

@demiankatz demiankatz modified the milestones: 9.1, 10.0 Oct 3, 2023
@demiankatz
Copy link
Member

I'm moving this to the 10.0 milestone, as we're getting close to the 9.1 code freeze, and I don't think this is going to get done in time. Of course, happy to keep working on it regardless whenever you're ready!

@demiankatz
Copy link
Member

@RLangeUni / @elsenhans, now that 10.0 development is beginning, I'm just checking in to see where we are on this PR. Please let me know if you need any action on my end to get this moving again.

@elsenhans
Copy link
Contributor

@RLangeUni , @demiankatz
If someone can tell me what is still missing or needs to be adjusted here, I would continue working on the PR. However, I am a frontend developer and it seems that the problems regarding the ID have to be solved mainly with PHP. Is that correct?

@demiankatz
Copy link
Member

@elsenhans, see my review comments above for the specific issues I highlighted on my previous review. As you say, all of this is touching PHP code, but it is PHP code that is used to set values for building the front-end templates, so it's definitely front-end-adjacent. Perhaps a conversation with @RLangeUni would be in order... or, if you'd like me to make some of my proposed changes so that you can test them, that's also an option (though I probably won't get to it quickly due to my long backlog of reviews at the moment).

@elsenhans
Copy link
Contributor

elsenhans commented Dec 18, 2023

@RLangeUni has informed me about the current status of the PR. The ID is already provided and the access to the ID in the templates should be implemented in a separate PR.
The comments and the error are still being worked on in this PR. For the changes of templates we would create a new PR.

@demiankatz
Copy link
Member

Thanks, @elsenhans -- then I'll just wait for the work to be completed on my earlier review comments. If I can do anything to help in the meantime, please let me know!

@demiankatz
Copy link
Member

@elsenhans / @RLangeUni, with the 10.0 release beginning to draw near, do you expect to do more work on this soon, or should we reschedule for a future release? I also notice that there is a conflict in the test class -- please let me know if you need my help to resolve that.

jpkanter added a commit to finc/vufind that referenced this pull request Apr 3, 2024
* added basic exception for 'getUniqueIdWithSourcePrefix' method
* changed 'getCheckbox'  variable generation
RLangeUni and others added 3 commits April 3, 2024 15:11
* added basic exception for 'getUniqueIdWithSourcePrefix' method
* changed 'getCheckbox'  variable generation
@jpkanter jpkanter force-pushed the pull-request-unique-html-id branch from 7c4a3f7 to 3d33b82 Compare April 3, 2024 13:13
* added workaround for phpUnit consecutive test
@jpkanter
Copy link
Contributor

jpkanter commented Apr 3, 2024

As @RLangeUni is on vacation this week i quickly tried to take over the open points but my lack of knowledge with PHPUnit made it impossible to replace the correct withConsecutive Call. Is next week early enough?

Copy link
Member

@demiankatz demiankatz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jpkanter, thanks for the progress here. I haven't fully reviewed your changes yet since I'm not sure if you're completely done yet, but I noticed that your test changes may be able to be simplified. See below...

* @param ...$args
* @return callable
*/
public function consecutiveCalls(...$args): callable
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might find it helpful to use WithConsecutiveTrait.php here -- that's the general-purpose solution I used to replace the deprecated withConsecutive method in other tests.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thats an interesting headsup, i will look into it tomorrow. I am indeed not done, i quickly threw some code together as the necessary changes seemed rather simple and i had no running test-environment on my local machine. As i cannot continue to spam not working tests i should probably get that going again.

@demiankatz
Copy link
Member

@jpkanter, this can definitely wait a week or two if necessary. But if you need help adapting the test to use WithConsecutiveTrait as I suggested, let me know and I don't mind helping. :-)

Copy link
Member

@demiankatz demiankatz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple of other small details...

$context = compact('number') + [
'id' => $this->getUniqueIdWithSourcePrefix(),
'checkboxElementId' => $this->getUniqueHtmlElementId($idPrefix)
];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor style point: I'd de-indent the closing ]; here for consistency with code style elsewhere.

@robertlange81
Copy link

I implemented a unique result set identifier for records, ensuring that each record receives a UUID-like identifier to avoid conflicts. PHPStn forced me to add the $recordCollection property in AbstractBackend. generateUuid() could be more handsome and moved to some Utils class? But I didn't found an existing appropriate. The template changes should not be merged. They are more for my own comparism. For example in result-list should be later changed like <a id="<?=$this->escapeHtmlAttr($this->record($this->driver)->getUniqueHtmlElementId())?>" ... - so it seems that there are no duplicate ids in the combined search.

Copy link
Member

@demiankatz demiankatz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @RLangeUni! I think we're nearly done here, but I think the phpstan issue you reported may have been related to some orphaned and unnecessary code; see below for details.

*/
public function setResultSetIdentifier(string $uuid)
{
$this->recordCollection->setResultSetIdentifier($uuid);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this method actually used? I think the property you were forced to add was related to the call in this method, but I suspect this method is unnecessary.

@robertlange81
Copy link

In the AbstractBackend class, I already had a slight suspicion that I was too eager. It seems "setResultSetIdentifier" is sufficient for the records/collections. Is the generation of the UUID alright? With Doctrine, one could also use its helper methods (Doctrine\ORM\Id\UuidGenerator).

@robertlange81
Copy link

robertlange81 commented Oct 18, 2024

"injectSourceIdentifier" is also called from InjectSpellingListener.php - should we check within AbstractBackend::injectSourceIdentifier() whether $response->getRecords() is empty or zero before setting the result set identifier unnecessarily?

@demiankatz
Copy link
Member

"injectSourceIdentifier" is also called from InjectSpellingListener.php - should we check within AbstractBackend::injectSourceIdentifier() whether $response->getRecords() is empty or zero before setting the result set identifier unnecessarily?

I suppose it wouldn't hurt to count the records before generating the UUID to potentially save a few cycles, though I don't think it's a huge issue.

Copy link
Member

@demiankatz demiankatz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See below for a few more minor suggestions!

*
* @var string|null
*/
protected $resultSetIdentifier;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest explicitly initializing this to null.

"/\s+/",
'_',
($idPrefix ? $idPrefix . '-' : '')
. $this->driver->getResultSetIdentifier() . '-'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add handling here to ensure this value is non-null, since concatenating null into a string may cause a notice. It would probably be good to add a case to the unit test to cover the "null record set identifier" case so we can ensure it behaves as expected.

@@ -116,6 +119,27 @@ abstract public function getRecordCollectionFactory();
protected function injectSourceIdentifier(RecordCollectionInterface $response)
{
$response->setSourceIdentifiers($this->identifier);
$response->setResultSetIdentifier($this->generateUuid());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this approach is fine for now, but maybe it's worth putting a TODO comment here to switch to using the Doctrine UUID generator once it becomes available to us in the future. Then we could potentially address that as part of (or sometime after the merge of) #2233.

@demiankatz
Copy link
Member

I'm also requesting a review from @maccabeelevine since #3552 is going to depend on the work here, so his opinion/input may be valuable!

@robertlange81
Copy link

Adopted the last suggestions. Thanks for your support!

Copy link
Member

@demiankatz demiankatz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @RLangeUni, this all looks good to me now. I appreciate your persistence in seeing this through to the end. :-)

I'm not going to merge it quite yet, just in case anyone else has last-minute feedback. I'll resolve it next week if there is no conversation before then.

Copy link
Member

@maccabeelevine maccabeelevine left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works very nicely!

@demiankatz
Copy link
Member

Thanks, @RLangeUni and @maccabeelevine. @EreMaijala pointed out that it might be better to generate real UUIDs with the ramsey/uuid library to prevent confusion. I'm inclined to agree but don't want to further delay this PR, so I'll merge this as-is and open a separate UUID PR shortly.

@demiankatz demiankatz merged commit 5af2250 into vufind-org:dev Oct 21, 2024
6 checks passed
@demiankatz demiankatz deleted the pull-request-unique-html-id branch October 21, 2024 12:34
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.

7 participants