Hani, nice work with this fieldtype, as well as the state select one. I tried both out and they work well!
I should explain why PW doesn't implement selects in the manner that this plugin does. This plugin stores values are not normalized; it uses the database as a non-relational flat file. One of the resulting issues if that if you need to go back and change or remove one of the options, your change will not affect any values already present, ultimately corrupting the data. This could be a problem in large scale usage. PW uses a Page fieldtype to achieve the same thing (albeit with a little more effort) so as to avoid the issues mentioned above and others. But the reality is that many sites are not working at that scale or affected by the drawbacks, so I think your Select fieldtype and it's simpler administration will be a welcome solution for many. Thank you for putting it together. I know you likely produced this fieldtype with an understanding of the compromises, so just wanted to highlight the considerations for others that may researching which solution to use in a given situation. I can certainly think of situations where I will use your fieldtype.
Down into the actual fieldtype's code, I have a few suggestions for you:
In your getInputfield() method, these two lines are not necessary:
$inputfield->attr('name', $field->name);
$inputfield->class = $this->className();
On my installation, the submitted values were getting stored with the "\n" as part of them. In your getInputfield() method, I recommend trimming the value before sending it to $inputfield->addOption(), i.e.
foreach($options as $option) {
$option = trim($option);
if(!strlen($option)) continue;
$inputfield->addOption($option);
}
When I edited a page with this fieldtype, it already had a value selected. I recommend adding a blank option at the top of the select so that the first value isn't automatically selected (again, in your getInputfield() method):
$inputfield = $this->modules->get('InputfieldSelect');
$inputfield->addOption(''); // blank or unselected option
In your sanitizeValue() method, you may want to consider adding something that checks that the value is actually one of the allowed options. This is optional, but just an extra if you want it. There is some overhead in doing it, so you may decide it's worthwhile or not depending on what you want. But here's one way you could do it:
<?php
public function sanitizeValue(Page $page, Field $field, $value) {
// remove any leading/trailing whitespace
$value = trim($value);
// If value isn't present in the select_options string, surrounded by CRLFs or CRs then it's not valid.
// We also allow for match at beginning/end of select_options, as well as optional spaces in the select_options string
// before and after the CRLF/CR that separate the options in the string.
if(!preg_match('/(^|[\r\n]) *' . preg_quote($value, '/') . ' *([\r\n]+|$)/', $field->select_options)) $value = '';
return $value;
}