Created
July 5, 2016 12:07
-
-
Save engram-design/ae0e93ed198f01c78b9e5ab24b1a3497 to your computer and use it in GitHub Desktop.
Custom sorting using `postForFeedMeFieldType` with Feed Me
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Craft; | |
class FeedMeCustomSortPlugin extends BasePlugin | |
{ | |
// ========================================================================= | |
// PLUGIN INFO | |
// ========================================================================= | |
public function getName() | |
{ | |
return Craft::t('Feed Me Custom Sort'); | |
} | |
public function getVersion() | |
{ | |
return '1.0.0'; | |
} | |
public function getSchemaVersion() | |
{ | |
return '1.0.0'; | |
} | |
public function getDeveloper() | |
{ | |
return 'S. Group'; | |
} | |
public function getDeveloperUrl() | |
{ | |
return 'http://sgroup.com.au'; | |
} | |
// ========================================================================= | |
// HOOKS | |
// ========================================================================= | |
public function postForFeedMeFieldType(&$fieldData) | |
{ | |
// This is less intensive than craft()->fields->getFieldByHandle($fieldHandle); | |
foreach ($fieldData as $fieldHandle => $data) { | |
if (is_array($data)) { | |
// If we've got an array of data, a good chance its a Matrix field - double-check! | |
$field = craft()->fields->getFieldByHandle($fieldHandle); | |
if ($field->type == 'Matrix') { | |
$orderedMatrixData = array(); | |
$tempMatrixData = array(); | |
foreach ($data as $key => $subField) { | |
if (isset($subField['fields']['order'])) { | |
$order = $subField['fields']['order']; | |
} else { | |
$order = $subField['order']; | |
} | |
$tempMatrixData[$order][$key] = $subField; | |
} | |
// Sort the array by key | |
ksort($tempMatrixData); | |
$fieldData[$fieldHandle] = array(); | |
foreach ($tempMatrixData as $key => $subField) { | |
$fieldData[$fieldHandle] = array_merge($fieldData[$fieldHandle], $subField); | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this to a folder called
feedmecustomsort
in your plugins folder.