-
Notifications
You must be signed in to change notification settings - Fork 396
Get unique html element id from record view helper #2999
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
Changes from 24 commits
c69cbc0
4859ace
3d33b82
3dd8446
22e7630
cbdedf7
3e308f2
e294a75
c05492d
0c34fbc
07af7fa
60de01a
d8dfc5e
d4a6492
6c57f53
3a9c45f
1848009
bd13052
6ee8f92
010756c
634dacb
898df17
48e5a10
939411f
87480db
d31c992
d614377
3a9b9d5
8cba230
46b2f01
83f543d
0ee81bc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -560,10 +560,11 @@ public function getSearchResult($view) | |
| */ | ||
| public function getCheckbox($idPrefix = '', $formAttr = false, $number = null) | ||
| { | ||
| $id = $this->driver->getSourceIdentifier() . '|' | ||
| . $this->driver->getUniqueId(); | ||
| $context | ||
| = ['id' => $id, 'number' => $number, 'prefix' => $idPrefix]; | ||
| $context = compact('number') + [ | ||
| 'id' => $this->getUniqueIdWithSourcePrefix(), | ||
| 'checkboxElementId' => $this->getUniqueHtmlElementId($idPrefix), | ||
| 'prefix' => $idPrefix, | ||
| ]; | ||
| if ($formAttr) { | ||
| $context['formAttr'] = $formAttr; | ||
| } | ||
|
|
@@ -857,4 +858,36 @@ protected function deduplicateLinks($links) | |
| { | ||
| return array_values(array_unique($links, SORT_REGULAR)); | ||
| } | ||
|
|
||
| /** | ||
| * Get the source identifier + unique id of the record without spaces | ||
| * | ||
| * @param string $idPrefix Prefix for HTML ids | ||
| * | ||
| * @return string | ||
| */ | ||
| public function getUniqueHtmlElementId($idPrefix = '') | ||
| { | ||
| return preg_replace( | ||
| "/\s+/", | ||
| '_', | ||
| ($idPrefix ? $idPrefix . '-' : '') | ||
| . $this->driver->getResultSetIdentifier() . '-' | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
| . $this->driver->getUniqueId() | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Get the source identifier + unique id of the record | ||
| * | ||
| * @return string | ||
| */ | ||
| public function getUniqueIdWithSourcePrefix() | ||
| { | ||
| if ($this->driver) { | ||
|
demiankatz marked this conversation as resolved.
|
||
| return "{$this->driver->getSourceIdentifier()}" | ||
| . "|{$this->driver->getUniqueId()}"; | ||
| } | ||
| throw new \Exception('No record driver found.'); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,9 +30,12 @@ | |
| namespace VuFindSearch\Backend; | ||
|
|
||
| use Laminas\Log\LoggerAwareInterface; | ||
| use Laminas\Math\Rand; | ||
| use VuFindSearch\Response\RecordCollectionFactoryInterface; | ||
| use VuFindSearch\Response\RecordCollectionInterface; | ||
|
|
||
| use function sprintf; | ||
|
|
||
| /** | ||
| * Abstract backend. | ||
| * | ||
|
|
@@ -53,6 +56,13 @@ abstract class AbstractBackend implements BackendInterface, LoggerAwareInterface | |
| */ | ||
| protected $collectionFactory = null; | ||
|
|
||
| /** | ||
| * Record collection. | ||
| * | ||
| * @var RecordCollectionInterface|null | ||
| */ | ||
| protected $recordCollection = null; | ||
|
|
||
| /** | ||
| * Backend identifier. | ||
| * | ||
|
|
@@ -116,6 +126,39 @@ abstract public function getRecordCollectionFactory(); | |
| protected function injectSourceIdentifier(RecordCollectionInterface $response) | ||
| { | ||
| $response->setSourceIdentifiers($this->identifier); | ||
| $response->setResultSetIdentifier($this->generateUuid()); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
|
|
||
| return $response; | ||
| } | ||
|
|
||
| /** | ||
| * Sets the result set identifier for the record collection. | ||
| * | ||
| * @param string $uuid A valid UUID associated with the data set. | ||
| * | ||
| * @return void | ||
| */ | ||
| public function setResultSetIdentifier(string $uuid) | ||
| { | ||
| $this->recordCollection->setResultSetIdentifier($uuid); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
| } | ||
|
|
||
| /** | ||
| * Generates a shorter UUID-like identifier. | ||
| * | ||
| * This method uses Laminas\Math\Rand to generate cryptographically secure random bytes | ||
| * and formats them into a shorter identifier. | ||
| * | ||
| * @return string A randomly generated shorter UUID-like identifier. | ||
| */ | ||
| public function generateUuid(): string | ||
| { | ||
| $data = bin2hex(Rand::getBytes(8)); | ||
| return sprintf( | ||
| '%08s-%04s-%04s', | ||
| substr($data, 0, 8), | ||
| substr($data, 8, 4), | ||
| substr($data, 12, 4) | ||
| ); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -47,6 +47,16 @@ trait RecordTrait | |
| */ | ||
| protected $sourceIdentifier = ''; | ||
|
|
||
| /** | ||
| * The unique identifier for the result set. | ||
| * | ||
| * This property stores a UUID or similar identifier that uniquely identifies | ||
| * the result set. It is typically set by calling the `setResultSetIdentifier` method. | ||
| * | ||
| * @var string|null | ||
| */ | ||
| protected $resultSetIdentifier; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd suggest explicitly initializing this to null. |
||
|
|
||
| /** | ||
| * Used for identifying the search backend used to find the record | ||
| * | ||
|
|
@@ -110,6 +120,33 @@ public function getSearchBackendIdentifier() | |
| return $this->searchBackendIdentifier; | ||
| } | ||
|
|
||
| /** | ||
| * Sets the unique result set identifier. | ||
| * | ||
| * This method assigns a UUID or similar identifier to the result set. | ||
| * | ||
| * @param string $uuid A valid UUID or identifier to assign to the result set. | ||
| * | ||
| * @return void | ||
| */ | ||
| public function setResultSetIdentifier(string $uuid) | ||
| { | ||
| $this->resultSetIdentifier = $uuid; | ||
| } | ||
|
|
||
| /** | ||
| * Retrieves the unique result set identifier. | ||
| * | ||
| * This method returns the UUID or similar identifier associated with the result set. | ||
| * If no identifier has been set, it will return null. | ||
| * | ||
| * @return string|null The UUID of the result set, or null if not set. | ||
| */ | ||
| public function getResultSetIdentifier(): ?string | ||
| { | ||
| return $this->resultSetIdentifier; | ||
| } | ||
|
|
||
| /** | ||
| * Add a label for the record | ||
| * | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.