<commit_before><?php

/**
 * This component creates a dropdown of possible page types and a button to create a new page.
 *
 * This bypasses GridFieldDetailForm to use the standard CMS.
 *
 * @package silverstripe
 * @subpackage lumberjack
 *
 * @author <NAME> <<EMAIL>>
 */
class GridFieldSiteTreeAddNewButton extends GridFieldAddNewButton
	implements GridField_ActionProvider {


	public function getAllowedChildren(SiteTree $parent) {
		$allowedChildren = $parent->allowedChildren();
		if(empty($allowedChildren)) return array();

		$children = array();
		foreach($allowedChildren as $class) {
			if(Config::inst()->get($class, "show_in_sitetree") === false) {
				$children[$class] = Injector::inst()->create($class)->i18n_singular_name();
			}
		}
		return $children;
	}

	public function getHTMLFragments($gridField) {
		$model = Injector::inst()->create($gridField->getModelClass());
		$parent = SiteTree::get()->byId(Controller::curr()->currentPageID());

		if(!$model->canCreate()) {
			return array();
		}

		$children = $this->getAllowedChildren($parent);
		if(count($children) > 1) {
			$pageTypes = DropdownField::create("PageType", "Page Type", $children, $model->defaultChild());
			$pageTypes->setFieldHolderTemplate("GridFieldSiteTreeAddNewButton_holder")->addExtraClass("gridfield-dropdown no-change-track");

			if(!$this->buttonName) {
				$this->buttonName = _t('GridFieldSiteTreeAddNewButton.AddMultipleOptions', 'Add new', "Add button text for multiple options.");
			}
		} else {
			$keys = array_keys($children);
			$pageTypes = HiddenField::create('PageType', 'Page Type', $keys[0]);

			if(!$this->buttonName) {
				$this->buttonName = _t('GridFieldSiteTreeAddNewButton.Add', 'Add new {name}', 'Add button text for a single option.', array($children[$keys[0]]));
			}
		}

		$state = $gridField->State->GridFieldSiteTreeAddNewButton;
		$state->currentPageID = $parent->ID;
		$state->pageType = $parent->defaultChild();

		$addAction = new GridField_FormAction($gridField, 'add', $this->buttonName, 'add', 'add');
		$addAction->setAttribute('data-icon', 'add')->addExtraClass("no-ajax ss-ui-action-constructive dropdown-action");

		$forTemplate = new ArrayData(array());
		$forTemplate->Fields = new ArrayList();
		$forTemplate->Fields->push($pageTypes);
		$forTemplate->Fields->push($addAction);
<commit_msg>FIX: Issue when only lumberjack pagetype is not defaultChild #7<commit_after><?php

/**
 * This component creates a dropdown of possible page types and a button to create a new page.
 *
 * This bypasses GridFieldDetailForm to use the standard CMS.
 *
 * @package silverstripe
 * @subpackage lumberjack
 *
 * @author <NAME> <<EMAIL>>
 */
class GridFieldSiteTreeAddNewButton extends GridFieldAddNewButton
	implements GridField_ActionProvider {


	public function getAllowedChildren(SiteTree $parent) {
		$allowedChildren = $parent->allowedChildren();
		if(empty($allowedChildren)) return array();

		$children = array();
		foreach($allowedChildren as $class) {
			if(Config::inst()->get($class, "show_in_sitetree") === false) {
				$children[$class] = Injector::inst()->create($class)->i18n_singular_name();
			}
		}
		return $children;
	}

	public function getHTMLFragments($gridField) {
		$model = Injector::inst()->create($gridField->getModelClass());
		$parent = SiteTree::get()->byId(Controller::curr()->currentPageID());

		if(!$model->canCreate()) {
			return array();
		}

		$state = $gridField->State->GridFieldSiteTreeAddNewButton;
		$state->currentPageID = $parent->ID;

		$children = $this->getAllowedChildren($parent);
		if(count($children) > 1) {
			$pageTypes = DropdownField::create("PageType", "Page Type", $children, $model->defaultChild());
			$pageTypes->setFieldHolderTemplate("GridFieldSiteTreeAddNewButton_holder")->addExtraClass("gridfield-dropdown no-change-track");

			$state->pageType = $parent->defaultChild();

			if(!$this->buttonName) {
				$this->buttonName = _t('GridFieldSiteTreeAddNewButton.AddMultipleOptions', 'Add new', "Add button text for multiple options.");
			}
		} else {
			$keys = array_keys($children);
			$pageTypes = HiddenField::create('PageType', 'Page Type', $keys[0]);

			$state->pageType = $keys[0];

			if(!$this->buttonName) {
				$this->buttonName = _t('GridFieldSiteTreeAddNewButton.Add', 'Add new {name}', 'Add button text for a single option.', array($children[$keys[0]]));
			}
		}

		$addAction = new GridField_FormAction($gridField, 'add', $this->buttonName, 'add', 'add');
		$addAction->setAttribute('data-icon', 'add')->addExtraClass("no-ajax ss-ui-action-constructive dropdown-action");

		$forTemplate = new ArrayData(array());
		$forTemplate->Fields = new ArrayList();
		$forTemplate->Fields->push($pageTypes);
		$forTemplate->Fields->push($addAction);

{'fraction_non_alphanumeric': 0.1673323523228926, 'fraction_numerical': 0.002522598276224511, 'mean_word_length': 5.590027700831025, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 74, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2887240', 'n_tokens_mistral': 1589, 'n_tokens_neox': 1443, 'n_words': 291}