Brad is correct – this isn't possible to do without writing a plugin. For your particular use case, this will be very simple, though.
What you'll have to do, is to write a custom FieldType that extends the stock EntriesFieldType class. The only thing your custom fieldtype needs to do, is to add an additional criteria to the query that pulls entries for input selection. Don't worry, this is going to be quite easy.
The quickest way to get started is to use the excellent Plugin Factory to scaffold your plugin. Call it whatever you like. Don't enable any of the plugin components.
Download and unzip your plugin, and put the folder in your craft/plugins
directory. Add a sub folder called fieldtypes
to your plugin's directory (i.e. {pluginhandle}/fieldtypes
).
Create a new file in the fieldtypes
directory called MyPlugin_RestrictedEntriesFieldType.php
or the like (where MyPlugin
should be replaced with the real name for your plugin).
Inside that file, paste the following code (remember to change the class name to whatever you called the file):
<?php
namespace Craft;
class MyPlugin_RestrictedEntriesFieldType extends EntriesFieldType
{
public function getName()
{
// This can be whatever you want, but avoid "Entries"
return Craft::t('Restricted Entries');
}
protected function getInputSelectionCriteria()
{
$user = craft()->userSession->getUser();
return array(
'authorId' => $user->id,
);
}
}
That's the whole thing; your custom fieldtype. Save the file and install your plugin. Convert whatever relevant Entries fields you may have to your new "Restricted Entries" fieldtype.
The code above should be fairly self explanatory – it extends EntriesFieldType which means it'll work exactly like the stock Entries field (and any fields can also be converted to and from the Entries fieldtype without losing data). The only difference is that your custom fieldtype includes the authorId
criteria when Craft pulls entries for input selection, where the authorId
needs to match the currently logged in user's ID.
If you need similar fields for other element types, you can just repeat the process – i.e. for a restricted Categories field, you'd create an additional file called myplugin/fieldtypes/MyPlugin_RestrictedCategoriesFieldType.php
that extends CategoriesFieldType
, and include the same getInputSelectionCriteria()
method as above.
For a bit of flexibility, you can also limit the restrictive behaviour to non-admins – i.e., if an admin is editing a post, that user should able to select entries authored by all users:
protected function getInputSelectionCriteria()
{
$user = craft()->userSession->getUser();
if (!$user->admin) {
return array(
'authorId' => $user->id,
);
}
return array();
}
Finally, a different option is to set the authorId
criteria to the current entry's (or category's) author's ID, instead of the currently logged in user's ID. This is more flexible and perhaps more future proof, in that any user that has permission to edit the entry will only be able to select entries that have the same author as the entry's original author.
Obviously, if the entry hasn't been saved yet it won't have an author, so this code should fall back to using the current user's ID for new entries, as above:
protected function getInputSelectionCriteria()
{
if ($this->element) {
return array(
'authorId' => $this->element->authorId,
);
}
$user = craft()->userSession->getUser();
return array(
'authorId' => $user->id,
);
}