How to get format settings of field in drupal 8? - field

I have a drupal 8 project and I was requested by my customer to get format settings of field in content type to respond to an api for mobile app. I created a new module, and trying to get data of format settings. But haven't succeed. I just get all setting of a field, but not display settings of field in content type.
On picture below, I want get all data of format column, and show it as a json. Please help me. Thanks so much!

If you want to extract the formatter settings for a specific field, you can use this option:
/** #var \Drupal\Core\Entity\EntityDisplayRepository $entityDisplayRepository */
$entityDisplayRepository = \Drupal::service('entity_display.repository');
$productViewDisplay = $entityDisplayRepository->getViewDisplay('node', 'page', 'teaser' /* optional */);
$all_formatters_settings = $productViewDisplay->get('content');
$formatter_settings = $all_formatters_settings['field_my_field'];
If you need to get the settings for the widget, you can similarly call the function getFormDisplay in the repository instead of getViewDisplay:
/** #var \Drupal\Core\Entity\EntityDisplayRepository $entityDisplayRepository */
$entityDisplayRepository = \Drupal::service('entity_display.repository');
$productViewDisplay = $entityDisplayRepository->getFormDisplay('node', 'page', 'teaser' /* optional */);
$all_formatters_settings = $productViewDisplay->get('content');
$formatter_settings = $all_formatters_settings['field_my_field'];

There is another way to get the formatter settings.
$formatter_settings = \Drupal::entityTypeManager()
->getStorage('entity_view_display')
->load("entity_type_id.entity_bundle.view_mode")
->getRenderer('field_logo_kataloga')
->getSettings();

Related

How to change token label using C_SetAttributeValue

Is there any way to change token label using C_SetAttributeValue? what template is being used to change token name as I tried with below function got error iaik.pkcs.pkcs11.wrapper.PKCS11Exception: CKR_TEMPLATE_INCOMPLETE
token = getToken();
CK_ATTRIBUTE[] attrs = new CK_ATTRIBUTE[2];
attrs[0] = new CK_ATTRIBUTE();
attrs[0].type = PKCS11Constants.CKA_LABEL;
attrs[0].pValue = label.toCharArray();
attrs[1] = new CK_ATTRIBUTE();
attrs[1].type = PKCS11Constants.CKA_ID;
attrs[1].pValue = label.toCharArray();
token.getSlot().getModule().getPKCS11Module().C_SetAttributeValue(
session.getSessionHandle(), token.getSlot().getSlotID(), attrs, true);
Hello on StackOverflow!
Have a look at C_SetAttributeValue definition:
CK_DEFINE_FUNCTION(CK_RV, C_SetAttributeValue)(
CK_SESSION_HANDLE hSession,
CK_OBJECT_HANDLE hObject,
CK_ATTRIBUTE_PTR pTemplate,
CK_ULONG ulCount
);
The second parameter is the Object ID, not the slot ID.
Please refer to your library's manufacturer documentation for extensions to PKCS#11 that allow to set token label.
C_SetAttributeValue is categorized as an object-management function. More precisely, the cryptoki function C_SetAttributeValue is used to modify or set an attribute value of an object (not token). If you use a standard PKCS#11 library, you should use C_initToken to change or set the token label.
Please note that a company may provide some non-standard functions for its own products. Thus, it might be also a non-standard function or extension in a specific product that helps you to change the token label.

Hiding custom ItemProperties from print. Interop.Outlook

I have written an Outlook plugin that basically allows emails being received through Outlook to be linked with a website so that the email can also be view in the communications feature of the website. I store additional details within the ItemProperties of a MailItem, these details are basically things like the id of the user the email relates to within a website.
The problem I'm having is any ItemProperties I add to a MailItem are being printed when the email is printed. Does anyone know how to exclude custom ItemProperties when printing an email?
Here is the code that is creating the custom ItemProperty:
// Try and access the required property.
Microsoft.Office.Interop.Outlook.ItemProperty property = mailItem.ItemProperties[name];
// Required property doesnt exist so we'll create it on the fly.
if (property == null) property = mailItem.ItemProperties.Add(name, Microsoft.Office.Interop.Outlook.OlUserPropertyType.olText);
// Set the value.
property.Value = value;
I'm working on Outlook extension and sometimes ago we had the same issue.
One of our team members found a solution. You can create some method which is responsible for disable printing. You can see peace of our code below:
public void DisablePrint()
{
long printablePropertyFlag = 0x4; // PDO_PRINT_SAVEAS
string printablePropertyCode = "[DispID=107]";
Type customPropertyType = _customProperty.GetType();
// Get current flags.
object rawFlags = customPropertyType.InvokeMember(printablePropertyCode , BindingFlags.GetProperty, null, _customProperty, null);
long flags = long.Parse(rawFlags.ToString());
// Remove printable flag.
flags &= ~printablePropertyFlag;
object[] newParameters = new object[] { flags };
// Set current flags.
customPropertyType.InvokeMember(printablePropertyCode, BindingFlags.SetProperty, null, _customProperty, newParameters);
}
Make sure that _customProperty it is your property which you created by the following code: mailItem.ItemProperties.Add(name,Microsoft.Office.Interop.Outlook.OlUserPropertyType.olText);
On the low (Extended MAPI) level, each user property definition has a flag that determines whether it is printable (namely, PDO_PRINT_SAVEAS). That flag however is not exposed through the Outlook Object Model.
You can either parse the user properties blob and manually set that flag (user properties blob format is documented, and you can see it in OutlookSpy (I am its author) if you click the IMessage button) or you can use Redemption (I am also its author) and its RDOUserProperty.Printable property.
The following script (VB) will reset the printable property for all user propeties of the currently selected message:
set Session = CreateObject("Redemption.RDOSession")
Session.MAPIOBJECT = Application.Session.MAPIOBJECT
set Msg = Session.GetMessageFromID(Application.ActiveExplorer.Selection(1).EntryID)
for each prop in Msg.UserProperties
Debug.Print prop.Name
prop.Printable = false
next
Msg.Save

how to validate and check if a entered URL is valid or not in Drupal 6

Without using any third party modules, is it possible to validate and check at the same time if any URL entered in a text-box is valid or not in Drupal 6 ? Some sample code will be appreciated.
menu_valid_path() function in menu.inc (which is part of Drupal Core) does just that.
To answer your specific question:
Without using any third party modules, is it possible to validate and check at the same time if any URL entered in a text-box is valid or not in Drupal 6
Yes.
You will, however, need to create a simple custom module.
Let's assume:
Your form id is my_form_1
The field name in question is my_path_field_1
In your MODULENAME.module file:
<?php
/**
* Modifies the existing form element 'my_path_field_1' to add
* 'MODULENAME_path_validate' function to validation array.
*
* (MYMODULE_path_validate is defined below)
*/
function MODULENAME_form_alter(&$form, $form_state, $form_id) {
switch ($form_id) {
case 'my_form_1' :
$form['my_path_field_1']['#element_validate'] = array('MODULENAME_path_validate');
break;
}
// Note, you could use hook_form_FORM_ID_alter(&$form, &$form_state)
// instead of the above to simplify things if the only thing this module
// does is validite one field for a valid path.
/**
* Validates the my_path_field_1 using Drupal's built-in menu_valid_path()
* function. Returns a form error if the field does not contain a valid path
* or the current user does not have access to the path's permission.
*/
function MODULENAME_path_validate($element, &$form_state) {
if (!menu_valid_path($element)) {
form_error($element, t('The path entered does not exist or you do not have permission to access it.'));
}
}

jQuery autocomplete not displaying my encoded values

I am working from this example: http://jqueryui.com/demos/autocomplete/#remote and I am encoding the output like this:
$rows = array();
while($r = mysql_fetch_assoc($category_result))
{
$rows[] = $r;
error_log ("rows: ".$rows[0]);
}
echo json_encode($rows);
But the dropdown on the other side shows nothing. Here is my test page: http://problemio.com/test.php - if you enter "ho" it matches 2 results in the database, but they are not getting displayed for some reason. Any idea why?
Thanks!!
The properties should be named label and value. From the JQuery UI demo page you linked to:
The local data can be a simple Array of Strings, or it contains
Objects for each item in the array, with either a label or value
property or both. The label property is displayed in the suggestion
menu.
So you would need to rename category_name to label either in PHP or later on in your JavaScript source handler function. The latter would require you to replace the PHP URL with a callback function like in the remote example. That way you could get the data any way you want (e.g. by jQuery.getJSON()) and work with it before it gets handed over to the suggestion box.
Hope this helps.
Regarding your comment, this should do it:
$rows = array();
while ($r = mysql_fetch_array($category_result)) {
$rows[] = array("label" => $r["category_name"]);
}
echo json_encode($rows);

Symfony embedRelation options - setting sub-form field visibility

The prototype of embedRelation makes reference to an 'options' array (passed as $formArguments/$formargs).
Is it possible to pass an options array:
embedRelation("Model","ModelForm",$options_arr);
Where the options_arr contains form validator/widgets/etc to set for the relation?
$formargs['something']['publish_date'] = new sfWidgetFormInputText();
Or is it possible to limit the form fields displayed for a relation in a way like this?
$formargs['something']['use_fields'] = array('publish_date');
From sfFormDoctrine.class.php
...
* Embed a Doctrine_Collection relationship in to a form
*
* [php]
* $userForm = new UserForm($user);
* $userForm->embedRelation('Groups AS groups');
*
* #param string $relationName The name of the relation and an optional alias
* #param string $formClass The name of the form class to use
* #param array $formArguments Arguments to pass to the constructor (related object will be shifted onto the front)
* #param string $innerDecorator A HTML decorator for each embedded form
* #param string $decorator A HTML decorator for the main embedded form
*
* #throws InvalidArgumentException If the relationship is not a collection
*/
public function embedRelation($relationName, $formClass = null, $formArgs = array(), $innerDecorator = null, $decorator = null)
...
The closest I've been able to get to a spec for the $formArgs array() is from sfFormPropel.class.php (I'm using doctrine 1.2):
* `title`: The title of the collection form once embedded. Defaults to the relation name.
* `embedded_form_class`: The class name of the forms to embed. Uses the model name by default (a form based on a collection of Book objects embeds BookForm objects).
* `collection_form_class`: Class of the collection form to return. Defaults to sfFormPropelCollection.
* `hide_on_new`: If true, the relation form does not appear for new objects. Defaults to false.
* `add_empty`: Whether to allow the user to add new objects to the collection. Defaults to true.
* `add_delete`: Whether to add a delete widget for each object. Defaults to true.
* `remove_fields`: The list of fields to remove from the embedded object forms
* `item_pattern`: The pattern used to name each embedded form. Defaults to '%index%'.
If `add_empty` is set to `true`, the following additional options are available:
* `empty_label`: The label of the empty form. Defaults to 'new' + the relation name.
* `add_link`: The text of the JavaScript link that displays the empty form. Defaults to `Ann new`
* `max_additions`: The max number of additions accepted on the client side. Defaults to 0 (no limit)
If `add_delete` is set to `true`, the following additional options are available:
* `delete_name`: Name of the delete widget. Defaults to 'delete'.
* `delete_widget`: Optional delete widget object. If left null, uses a `sfWidgetFormDelete` instance, which is a checkbox widget with a Javascript confirmation.
* `alert_text`: The text of the Javascript alert to show.
* `hide_parent`: Whether to hide the deleted form when clicking the checkbox. Defaults to true.
* `parent_level`: The number of times parentNode must be called to reach the parent to hide. As a widget doesn't know if it's merged or embedded, this setting allows the JavaScript code used to hide the parent to find it. Recommended values: 6 for embedded form (default), 7 for merged form.
Any insight would be greatly appreciated.
I think that if you put say array('toto' => 'pwet') as form args, you will be able to retrieve 'pwet' in your form using $this->getOption('toto'); From there everything is possible (setting widgets and validators)
Why don't you go another way:
$o = $this->isNew() ? new Model() : $this->getObject()->getModel();
$model_form = new ModelForm($o);
//now configure widgets and validators
$model_form->setWidget('publish_date', new sfWidgetFormInputText());
$model_form->useFields(array('publish_date'));
$this->embedForm('Model', $model_form);

Resources