Jump to content

Single ASMSelect in Module


Pete
 Share

Recommended Posts

Hi guys

I'm tweaking a module of mine (SocialTwitterFeed) to do something else and need to have an ASMSelect in the config screen -this bit is simple enough, however how do I limit it so the user can only select ONE page?

Here's the current code:

private static function _createInputfieldASMSelect($aName, $aTitle, $aValue, $aDesc='', $aPath='/') {
	if(!isset($aValue) || !is_array($aValue)) $aValue = array();
	$modules = Wire::getFuel('modules');
	$field = $modules->get("InputfieldAsmSelect");
	$field->attr('name', $aName);
	$children = wire('pages')->get($aPath)->children;
	foreach($children as $child) {
		$field->addOption($child->id, $child->title);
	}
	$field->attr('value', $aValue);
	$field->label = $aTitle;
	$field->description = $aDesc;
	return $field;
}

In this case, it's being used to select a field, so here's the more relevant code that works, but currently selects multiple fields instead of just one:

private static function _createInputfieldASMFieldSelect($aName, $aTitle, $aValue, $aDesc='', $aPath='/') {
	if(!isset($aValue) || !is_array($aValue)) $aValue = array();
	$modules = Wire::getFuel('modules');
	$field = $modules->get("InputfieldAsmSelect");
	$field->attr('name', $aName);
	$children = wire('fields')->find('id>1');
	foreach($children as $child) {
		$field->addOption($child->id, $child->name);
	}
	$field->attr('value', $aValue);
	$field->label = $aTitle;
	$field->description = $aDesc;
	return $field;
}

I posted both as thought the latter might be useful to someone already familiar with the former, and I'd be interested to see if there are any major differences required for the two.

Cheers!

Oh, and the addition is going to be that you can specify a field for the module so that you can enter a customised message for a given news item/article/whatever to accompany the title and link in the twitter post on a per-page basis rather than the blanket default message in the current version - though it'll fall back to the default message if no custom message is selected.

Getting complicated, but very configurable :P;)

Link to comment
Share on other sites

Cool!

I think you only need this line:

$field->derefAsPage = FieldtypePage::derefAsPageOrFalse;

Edit: just reading again... you want to select fields not pages? Why not just use a simple select?

I think ASM select and the others depend on the fieldtype page, which hold the setting to select multiple or only one. Not sure if there's a way to use ASM select for this, as it's always possible to select multiple.. just 1 will be saved.

I need to think again what you're really trying :)

Edit: oh and in modules you can always just use. $this->modules->get("nameofmodule").

Edit: I recently used something like this in my DataTable module, maybe this is of help too.


$field = $this->modules->get("InputfieldSelect"); 
$field->attr('id+name', 'filter_template'); 
$field->label = "Filter by Template";
$field->description = "....";
$field->addOption('', 'Show All');

foreach($this->templates as $t) {
  $name = $t->name;
  if(($t->flags & Template::flagSystem)) $name .= "*";
  if($this->optionTemplate == $t->name) { // optionTemplate is just example, depending on how you save the settings in your case
	 $field->addOption($t->name, $name, array("selected"=>"selected"));
  } else {
	 $field->addOption($t->name, $name);
  }
  }
}
  • Like 2
Link to comment
Share on other sites

Edit: just reading again... you want to select fields not pages? Why not just use a simple select?

Oh yeah :-[

I think I've just been going ASMSelect crazy recently because it looks so nice and overlooked the obvious solution :lol: I think in my case since the site I'm working on has about 30 fields so far it might be better for me to use autocomplete, but I've not got so many that a normal select wouldn't be the easiest and best option. Plus people will think I've gone crazy if I make it an autocomplete field and most sites don't have that many fields (an autocomplete for a dozen fields would be silly for example).

Thanks for all of the suggestions though Soma - very useful indeed!

Link to comment
Share on other sites

And here's my function for the single drop-down module config field if it's useful to anyone.

private static function _createInputfieldSelect($ipName, $ipTitle, $ipValue='', $ipDesc=''){
       $field = wire('modules')->get('InputfieldSelect');
       $field->name = $ipName;
       $field->label = $ipTitle;
       $children = wire('fields')->find('id>1');
       foreach($children as $child) {
           $field->addOption($child->id, $child->name);
           if (!empty($ipValue)) {
               if ($child->id == $ipValue[0]) {
                   $field->attr('selected', 'selected');
               }
           }
       }
       $field->required = false;
       $field->description = $ipDesc;
       return $field;
   }
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...