Jump to content


Photo

Single ASMSelect in Module

Solved

  • Please log in to reply
3 replies to this topic

#1 Pete

Pete

    Administrator

  • Administrators
  • 1,802 posts
  • 727

  • LocationChester, England

Posted 15 March 2012 - 03:14 PM

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 ;)

#2 Soma

Soma

    Hero Member

  • Moderators
  • 3,421 posts
  • 1944

  • LocationSH, Switzerland

Posted 15 March 2012 - 03:22 PM

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);
	  }
   }
}

@somartist | modules created | support me, flattr my work flattr.com


#3 Pete

Pete

    Administrator

  • Administrators
  • 1,802 posts
  • 727

  • LocationChester, England

Posted 15 March 2012 - 03:47 PM

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!

#4 Pete

Pete

    Administrator

  • Administrators
  • 1,802 posts
  • 727

  • LocationChester, England

Posted 15 March 2012 - 04:06 PM

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;
    }






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users