Skip to content

Commit

Permalink
Merge branch '4.6' of https://github.com/craftcms/cms into 5.0
Browse files Browse the repository at this point in the history
# Conflicts:
#	CHANGELOG-WIP.md
#	src/helpers/ElementHelper.php
#	src/services/Elements.php
  • Loading branch information
brandonkelly committed Dec 11, 2023
2 parents d4e1f45 + 27cf344 commit 4bd3ada
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 29 deletions.
2 changes: 2 additions & 0 deletions src/base/FieldInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,8 @@ public function getSearchKeywords(mixed $value, ElementInterface $element): stri
* - If an existing element was retrieved from the database, the value will be whatever is stored in the field’s
* `content` table column. (Or if the field doesn’t have a `content` table column per [[hasContentColumn()]],
* the value will be `null`.)
* - If the field is being cleared out (e.g. via the `resave/entries` command with `--to :empty:`),
* the value will be an empty string (`''`).
*
* There are cases where a pre-normalized value could be passed in as well, so be sure to account for that.
*
Expand Down
2 changes: 1 addition & 1 deletion src/console/controllers/ResaveController.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ final public static function normalizeTo(?string $to): callable
// empty
if ($to === ':empty:') {
return function() {
return null;
return '';
};
}

Expand Down
17 changes: 17 additions & 0 deletions src/helpers/ElementHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,23 @@ public static function attributeHtml(mixed $value): string
return Html::encode(StringHelper::stripHtml($value));
}

/**
* Returns the searchable attributes for a given element, ensuring that `slug` and `title` are included.
*
* @param ElementInterface $element
* @return string[]
* @since 4.6.0
*/
public static function searchableAttributes(ElementInterface $element): array
{
$searchableAttributes = array_flip($element::searchableAttributes());
$searchableAttributes['slug'] = true;
if ($element::hasTitles()) {
$searchableAttributes['title'] = true;
}
return array_keys($searchableAttributes);
}

/**
* Returns a generic editor URL for the given element.
*
Expand Down
50 changes: 28 additions & 22 deletions src/services/Elements.php
Original file line number Diff line number Diff line change
Expand Up @@ -3255,6 +3255,7 @@ private function _saveElementInternal(
}
}

$fieldLayout = $element->getFieldLayout();
$dirtyFields = $element->getDirtyFields();

// Validate
Expand Down Expand Up @@ -3308,7 +3309,7 @@ private function _saveElementInternal(
$elementRecord->canonicalId = $element->getIsDerivative() ? $element->getCanonicalId() : null;
$elementRecord->draftId = (int)$element->draftId ?: null;
$elementRecord->revisionId = (int)$element->revisionId ?: null;
$elementRecord->fieldLayoutId = $element->fieldLayoutId = (int)($element->fieldLayoutId ?? $fieldLayout->id ?? 0) ?: null;
$elementRecord->fieldLayoutId = $element->fieldLayoutId = (int)($element->fieldLayoutId ?? $fieldLayout?->id ?? 0) ?: null;
$elementRecord->enabled = (bool)$element->enabled;
$elementRecord->archived = (bool)$element->archived;
$elementRecord->dateLastMerged = Db::prepareDateForDb($element->dateLastMerged);
Expand Down Expand Up @@ -3515,33 +3516,38 @@ private function _saveElementInternal(
}

// Update search index
if (
$updateSearchIndex &&
!$element->getIsRevision() &&
!ElementHelper::isRevision($element) &&
(!$trackChanges || !empty($dirtyAttributes) || !empty($dirtyFields))
) {
$event = new ElementEvent([
'element' => $element,
]);
$this->trigger(self::EVENT_BEFORE_UPDATE_SEARCH_INDEX, $event);
if ($event->isValid) {
if (Craft::$app->getRequest()->getIsConsoleRequest()) {
Craft::$app->getSearch()->indexElementAttributes($element);
} else {
Queue::push(new UpdateSearchIndex([
'elementType' => get_class($element),
'elementId' => $element->id,
'siteId' => $propagate ? '*' : $element->siteId,
'fieldHandles' => $dirtyFields,
]), 2048);
if ($updateSearchIndex && !$element->getIsRevision() && !ElementHelper::isRevision($element)) {
$searchableDirtyFields = array_filter(
$dirtyFields,
fn(string $handle) => $fieldLayout?->getFieldByHandle($handle)?->searchable,
);

if (
!$trackChanges ||
!empty($searchableDirtyFields) ||
!empty(array_intersect($dirtyAttributes, ElementHelper::searchableAttributes($element)))
) {
$event = new ElementEvent([
'element' => $element,
]);
$this->trigger(self::EVENT_BEFORE_UPDATE_SEARCH_INDEX, $event);
if ($event->isValid) {
if (Craft::$app->getRequest()->getIsConsoleRequest()) {
Craft::$app->getSearch()->indexElementAttributes($element, $searchableDirtyFields);
} else {
Queue::push(new UpdateSearchIndex([
'elementType' => get_class($element),
'elementId' => $element->id,
'siteId' => $propagate ? '*' : $element->siteId,
'fieldHandles' => $searchableDirtyFields,
]), 2048);
}
}
}
}

// Update the changed attributes & fields
if ($trackChanges) {
$dirtyAttributes = $element->getDirtyAttributes();
$userId = Craft::$app->getUser()->getId();
$timestamp = Db::prepareDateForDb(DateTimeHelper::now());

Expand Down
8 changes: 2 additions & 6 deletions src/services/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use craft\events\SearchEvent;
use craft\helpers\ArrayHelper;
use craft\helpers\Db;
use craft\helpers\ElementHelper;
use craft\helpers\Search as SearchHelper;
use craft\helpers\StringHelper;
use craft\models\Site;
Expand Down Expand Up @@ -179,12 +180,7 @@ public function indexElementAttributes(ElementInterface $element, ?array $fieldH
Db::delete(Table::SEARCHINDEX, $deleteCondition);

// Update the element attributes' keywords
$searchableAttributes = array_flip($element::searchableAttributes());
$searchableAttributes['slug'] = true;
if ($element::hasTitles()) {
$searchableAttributes['title'] = true;
}
foreach (array_keys($searchableAttributes) as $attribute) {
foreach (ElementHelper::searchableAttributes($element) as $attribute) {
$value = $element->getSearchKeywords($attribute);
$this->_indexKeywords($element, $value, attribute: $attribute);
}
Expand Down

0 comments on commit 4bd3ada

Please sign in to comment.