Skip to content

Instantly share code, notes, and snippets.

@engram-design
Created July 5, 2016 12:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save engram-design/ae0e93ed198f01c78b9e5ab24b1a3497 to your computer and use it in GitHub Desktop.
Save engram-design/ae0e93ed198f01c78b9e5ab24b1a3497 to your computer and use it in GitHub Desktop.
Custom sorting using `postForFeedMeFieldType` with Feed Me
<?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);
}
}
}
}
}
}
@engram-design
Copy link
Author

Add this to a folder called feedmecustomsort in your plugins folder.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment