Cake\View\Helper\FormHelper::label PHP Метод

label() публичный Метод

Will automatically generate a for attribute if one is not provided. ### Options - for - Set the for attribute, if its not defined the for attribute will be generated from the $fieldName parameter using FormHelper::_domId(). Examples: The text and for attribute are generated off of the fieldname echo $this->Form->label('published'); Custom text: echo $this->Form->label('published', 'Publish'); Custom attributes: echo $this->Form->label('published', 'Publish', [ 'for' => 'post-publish' ]); Nesting an input tag: echo $this->Form->label('published', 'Publish', [ 'for' => 'published', 'input' => $this->text('published'), ]); If you want to nest inputs in the labels, you will need to modify the default templates.
public label ( string $fieldName, string | null $text = null, array $options = [] ) : string
$fieldName string This should be "modelname.fieldname"
$text string | null Text that will appear in the label field. If $text is left undefined the text will be inflected from the fieldName.
$options array An array of HTML attributes.
Результат string The formatted LABEL element
    public function label($fieldName, $text = null, array $options = [])
    {
        if ($text === null) {
            $text = $fieldName;
            if (substr($text, -5) === '._ids') {
                $text = substr($text, 0, -5);
            }
            if (strpos($text, '.') !== false) {
                $fieldElements = explode('.', $text);
                $text = array_pop($fieldElements);
            }
            if (substr($text, -3) === '_id') {
                $text = substr($text, 0, -3);
            }
            $text = __(Inflector::humanize(Inflector::underscore($text)));
        }
        if (isset($options['for'])) {
            $labelFor = $options['for'];
            unset($options['for']);
        } else {
            $labelFor = $this->_domId($fieldName);
        }
        $attrs = $options + ['for' => $labelFor, 'text' => $text];
        if (isset($options['input'])) {
            if (is_array($options['input'])) {
                $attrs = $options['input'] + $attrs;
            }
            return $this->widget('nestingLabel', $attrs);
        }
        return $this->widget('label', $attrs);
    }

Usage Example

 /**
  * Override
  *
  * @param string $fieldName field name
  * @param string $text caption
  * @param array $options An array of HTML attributes.
  * @return string
  */
 public function label($fieldName, $text = null, array $options = [])
 {
     $options = $this->addClass($options, 'control-label');
     if ($this->_isHorizontal === true) {
         $options = $this->addClass($options, 'col-sm-2');
     }
     return parent::label($fieldName, $text, $options);
 }
All Usage Examples Of Cake\View\Helper\FormHelper::label