Skip to content

Instantly share code, notes, and snippets.

@engram-design
Last active August 29, 2015 14:22
Show Gist options
  • Save engram-design/266f56f17fa4700e14a0 to your computer and use it in GitHub Desktop.
Save engram-design/266f56f17fa4700e14a0 to your computer and use it in GitHub Desktop.
Swap Structures and Channels
<?php
namespace Craft;
class MyPluginNamePlugin extends BasePlugin
{
/* --------------------------------------------------------------
* PLUGIN INFO
* ------------------------------------------------------------ */
public function getName()
{
return Craft::t('My Plugin Name');
}
public function getVersion()
{
return '0.1';
}
public function getDeveloper()
{
return 'Somebody';
}
public function getDeveloperUrl()
{
return 'http://website.com';
}
/* --------------------------------------------------------------
* HOOKS
* ------------------------------------------------------------ */
public function modifyEntrySources(&$sources, $context)
{
if ($context == 'index')
{
$sections = craft()->sections->getEditableSections();
$editable = true;
}
else
{
$sections = craft()->sections->getAllSections();
$editable = false;
}
$sectionIds = array();
$singleSectionIds = array();
$sectionsByType = array();
foreach ($sections as $section)
{
$sectionIds[] = $section->id;
if ($section->type == SectionType::Single)
{
$singleSectionIds[] = $section->id;
}
else
{
$sectionsByType[$section->type][] = $section;
}
}
$sources = array(
'*' => array(
'label' => Craft::t('All entries'),
'criteria' => array('sectionId' => $sectionIds, 'editable' => $editable)
)
);
if ($singleSectionIds)
{
$sources['singles'] = array(
'label' => Craft::t('Singles'),
'criteria' => array('sectionId' => $singleSectionIds, 'editable' => $editable)
);
}
$sectionTypes = array(
SectionType::Structure => Craft::t('Structures'),
SectionType::Channel => Craft::t('Channels'),
);
foreach ($sectionTypes as $type => $heading)
{
if (!empty($sectionsByType[$type]))
{
$sources[] = array('heading' => $heading);
foreach ($sectionsByType[$type] as $section)
{
$key = 'section:'.$section->id;
$sources[$key] = array(
'label' => Craft::t($section->name),
'data' => array('type' => $type, 'handle' => $section->handle),
'criteria' => array('sectionId' => $section->id, 'editable' => $editable)
);
if ($type == SectionType::Structure)
{
$sources[$key]['structureId'] = $section->structureId;
$sources[$key]['structureEditable'] = craft()->userSession->checkPermission('publishEntries:'.$section->id);
}
}
}
}
return $sources;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment